An example of a palidrome is "avon sees nova"
There a number of ways in which we can find out if a string is a palidrome or not. Here are a few sample C programs...
Method1
#include < stdio.h >
#include < string.h >
#include < stdlib.h >
#include < ctype.h >
void isPalindrome(char *string);
int main()
{
isPalindrome("avon sees nova");
isPalindrome("a");
isPalindrome("avon sies nova");
isPalindrome("aa");
isPalindrome("abc");
isPalindrome("aba");
isPalindrome("3a2");
exit(0);
}
void isPalindrome(char *string)
{
char *start, *end;
if(string)
{
start = string;
end = string + strlen(string) - 1;
while((*start == *end) && (start!=end))
{
if(start < end)start++;
if(end > start)end--;
}
if(*start!=*end)
{
printf("\n[%s] - This is not a palidrome!\n", string);
}
else
{
printf("\n[%s] - This is a palidrome!\n", string);
}
}
printf("\n\n");
}
Method2
#include < stdio.h >
#include < string.h >
#include < stdlib.h >
#include < ctype.h >
int isPalindrome(char string[]);
int main()
{
isPalindrome("avon sees nova");
isPalindrome("a");
isPalindrome("avon sies nova");
isPalindrome("aa");
isPalindrome("abc");
isPalindrome("aba");
isPalindrome("3a2");
return(0);
}
int isPalindrome(char string[])
{
int count, countback, end, N;
N = strlen(string);
end = N-1;
for((count=0, countback = end); count <= (end/2); ++count,--countback)
{
if(string[count]!=string[countback])
{
return(1);
}
}
printf("\n[%s] is a palidrome!\n", string);
return(0);
}
Monday, July 9, 2007
Subscribe to:
Post Comments (Atom)
11 comments:
please can u just give a basic idea how to write a program to check a word is palindrome or not.. in simple
int flag=0;
for(i=0,j=strlen(str)-1;i!=j;i++,j--)
{
if(str[i]!=str[j])
{flag=1;break;}
}
if(flag==0)
printf("palindrome");
please help to check integer for palindrome
i think we can also use strrev function..
string==strrev(string);somprasad
vivekhas3 - I think there is an error in your solution. I only found this out because I ran into the same thing when creating my own solution. It won't work for an even number of chars since your for loop goes until i!=j. if i=0 at first and j=1, the next set of numbers is i=1, j=0. it would work if your for loop said this instead:
for(i=0,j=strlen(str)-1;i<j;i++,j--)
for(i=0;(a[i]=getchar())!=" ";i++)
;
a[i] = "\0";
for(j=0;a[i];j++) b[j]= a[i];
b[j] = "\0';
for(j=0,i=0;a[i];i++,j++)
if(a[i]!=b[j])
printf("Not palindrome\n %s",a);
else
printf("Palindrome\n %s",a);
I would like to agree with him/her ^^
I tried it, but it won't work properly. But the code he/she gave ain't good either. Sry to you both.
int isPalin(char *str){
i=0;
j = strlen(str) - 1;
while(i<j){
if(str[i++] != str[j--])
isPalin = 0;
}
if(isPalin == 1)
printf("Palindrome");
else
printf("not Palindrome");
}
Thanks for sharing your thoughts about kashmiri saffron.
Regards
Take a look at my weblog - Email Console
I’m not that much of a online reader to be honest but your blogs really nice, keep it up!
I'll go ahead and bookmark your site to come back later on. Many thanks
Take a look at my blog: diets that work
Normally I do not learn article on blogs, however I wish to
say that this write-up very pressured me to take a look at and do it!
Your writing style has been surprised me. Thank you, quite great post.
Here is my site: healthy waist to height ratio
Post a Comment