A string of length n always has at least n palindromes given that any string of length 1 is a palindrome. Thus we initialise the count to n and test all substrings of length 2 or more:
int count_palindromes (char* str) {
if (!str) return 0;
int n = strlen (str);
char* sub = malloc (n+1); // allocate memory for substring (+1 to include null-terminator)
memset (sub, 0, n+1); // zero the memory int count = n;
for (int len=2; len<=n; ++len) { // length of string (2 to n)
for (int i=0; i<=n-len; ++i) { // index of start character
memcpy (sub, str+i, len); // copy len characters from str+i
if (is_palindrome (sub)) ++count; // test the substring
}
free (sub);
sub = NULL;
return count;
}
Usage:
assert (count_palindromes ("racecar") == 10);
assert (count_palindromes ("abcde") == 5);
Note that counting palindromes in this manner is not generally useful. For every palindrome of length n>2 there has to be at least n+n/2 palindromes within it, and we can easily compute this figure without testing every substring. E.g., the palindrome "racecar" includes the palindromes "racecar", "aceca", "cec", "r", "a", "c", "e", "c", "a" and "r", but the only one we're actually interested in is "racecar" itself.
To achieve this we simply examine those substrings with either 2 or 3 characters. That is, when we find "cec" in the middle of "racecar", there's no need to test for "aceca" or "racecar" because "cec" is common to all three.
int count_palindromes (char* str) {
if (!str) return 0;
int n = strlen (str);
if (n<2) return 0;
char sub[4];
memset (&sub, 0, 4);
int count = n;
for (int len=2; len<=3; ++len) {
for (int i=0; i<n-len; ++i) {
memcpy (sub, str+i, len);
if (is_palindrome (sub)) ++count;
} return count;
}
Usage:
assert (count_palindromes ("racecar") == 1); // "cec"
assert (count_palindromes ("abbabcded") == 3); // "bb", "bab" and "ded"
The is_palindrome() function has the following implementation:
bool is_palindrome (char* str) { int x, y;
if (!str) return false;
int n = strlen (str);
if (n<2) return true; // empty strings and single character strings are always palindromes
x = 0; // point to first character
y = n-1; // point to last character
// work towards middle of string while characters are equal
while (x<y && str[x]==str[y]) ++x, --y;
return x>=y; // if the pointers met or passed one another, the string is a palindrome
}
use the strrev() function on the given string and compare with original string.If both are equal they are palindromes else not.
/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }
/*To check whether a string is palindrome*/includeincludevoid main () {int i,j,f=0;char a[10];clrscr ();gets(a);for (i=0;a[i]!='\0';i++){}i--;for (j=0;a[j]!='\0';j++,i--){if (a[i]!=a[j])f=1;}if (f==0)printf("string is palindrome");else printf("string is not palindrome");getch ();}
import java.util.Scanner; public class Palindrome{ public static void main(String[] args){ String front; String back =""; char[] failure; String backwards; Scanner input=new Scanner(System.in); System.out.print("Enter a word: "); front=input.next(); front=front.replaceAll(" ", ""); failure=front.toCharArray(); for (int i=0; i<failure.length; i++){ back=failure[i] + back; } if (front.equals(back)){ System.out.print("That word is a palindrome"); }else System.out.print("That word is not a palindrome"); }}
Private Sub Command1_Click() a = Text1.Text b = StrReverse(a) If a = b Then Print "palindrome" Else Print "not palindrome" End If End Sub
use the strrev() function on the given string and compare with original string.If both are equal they are palindromes else not.
Reverse the string and compare it to the original. If they match, then it is a palindrome.
Yes, you can use regex to determine if a given string is a palindrome by reversing the string and then comparing it to the original string using regex.
/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }
/*To check whether a string is palindrome*/includeincludevoid main () {int i,j,f=0;char a[10];clrscr ();gets(a);for (i=0;a[i]!='\0';i++){}i--;for (j=0;a[j]!='\0';j++,i--){if (a[i]!=a[j])f=1;}if (f==0)printf("string is palindrome");else printf("string is not palindrome");getch ();}
import java.util.Scanner; public class Palindrome{ public static void main(String[] args){ String front; String back =""; char[] failure; String backwards; Scanner input=new Scanner(System.in); System.out.print("Enter a word: "); front=input.next(); front=front.replaceAll(" ", ""); failure=front.toCharArray(); for (int i=0; i<failure.length; i++){ back=failure[i] + back; } if (front.equals(back)){ System.out.print("That word is a palindrome"); }else System.out.print("That word is not a palindrome"); }}
No. Palindromes are a form of word play, and malapropisms are oddly inappropriate words which seem to fit in a given context. But neither could be considered to be figures of speech.A palindrome is a literary device - it uses letters and words to create fanciful, elaborate, or intricate designs.
plz any1 give code on given exampleAs i know we can check String is it palindrome or not..take a look onCode_for_palindrome_checking_in_c_programmingorvoid main(){char strsrc[100];char strtmp[100];printf(" Enter String:= ");gets(strsrc);strcpy(strtmp,strsrc);strrev(strtmp);if(strcmp(strsrc,strtmp)==0)printf("\n Entered string " %s " is palindrome",strsrc);elseprintf("\n Entered string "%s" is not palindrome",strsrc);getch();}You cite 2 examples of what you are looking for so why are you asking the question? The palindrome is word "radar" so obviously the program needs to extract individual word from the string in order to test. That is a simple thing to do. I am not going to do it for you because if I do you do not learn what you need to know.....i did separated string by for (i=0 arr!=arr.lnegth i++){if(arr[i]==" ")} then each string checked for this code but that not right as persistence employee said..
Private Sub Command1_Click() a = Text1.Text b = StrReverse(a) If a = b Then Print "palindrome" Else Print "not palindrome" End If End Sub
#include <stdio.h> #include <conio.h> #include <string.h> void input(char a[ ]) { int i; printf("\n enter string\n"); scanf("%s",a); } void output(char a[ ]) { printf("\n string is %s",a); } int palindrome(char a[ ]) { int n,i; n=count(a); n=n-1; i=0; for(;a[n]==a[i] && n>=i;i++,n--); if(n>=i) return 0; else return 1; } void main( ) { char a[80],b[80],s; int n; printf("\n check palindrome"); input(a); n=palindrome(a); output(a); if(n==1) printf("\n palindrome"); else printf("\n not palindrome"); getch(); }
#include <stdio.h> #include <conio.h> #include <string.h> void input(char a[ ]) { int i; printf("\n enter string\n"); scanf("%s",a); } void output(char a[ ]) { printf("\n string is %s",a); } int palindrome(char a[ ]) { int n,i; n=count(a); n=n-1; i=0; for(;a[n]==a[i] && n>=i;i++,n--); if(n>=i) return 0; else return 1; } void main( ) { char a[80],b[80],s; int n; printf("\n check palindrome"); input(a); n=palindrome(a); output(a); if(n==1) printf("\n palindrome"); else printf("\n not palindrome"); getch(); }
#include <stdio.h> #include <conio.h> #include <string.h> void input(char a[ ]) { int i; printf("\n enter string\n"); scanf("%s",a); } void output(char a[ ]) { printf("\n string is %s",a); } int palindrome(char a[ ]) { int n,i; n=count(a); n=n-1; i=0; for(;a[n]==a[i] && n>=i;i++,n--); if(n>=i) return 0; else return 1; } void main( ) { char a[80],b[80],s; int n; printf("\n check palindrome"); input(a); n=palindrome(a); output(a); if(n==1) printf("\n palindrome"); else printf("\n not palindrome"); getch(); }