Pages

Testing Palindrome in C

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()
{
    Char word[25], revWord[25];
    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. 

@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment