Palindrome is a word, phrase, number or other sequence which has same form before and after reversing it for example mom, 5775 etc. Here if we read from front or back we get the same form. In this example we check Palindrome condition for string. We use strcpy(), strrev() and strcmp() function defined in string.h library in C to fulfill our requirements.
palindrome.c
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
printf("Enter word to test Palindrome : ");
gets(word); //get string content and store to word variable;
//copy word to revWord;
strcpy(revWord,word);
//reverse the content in revWord and store in it;
strrev(revWord);
//check for palindrome condition, returns 0 if both content are equal;
if(strcmp(revWord, word) == 0)
printf("%s is palindrome", word);
else
printf("%s is not palindrome", word);
getch();
}
Output:
Enter word to test Palindrome : mom
mom is palindrome.
Enter word to test Palindrome : hello
hello is not palindrome.
No comments:
Post a Comment