Posted By:
23 Jun, 2013 1:14 pm PDT

Palindrome in C

How to find whether a number or string is palindrome in C?
Attachment:None


To find whether a string is palindrome we first copy that string into a new string and then reverse that string and then compare with the original string. If both strings are same then it is palindrome. To find whether a number is palindrome we reverse the number and then compare with the original. If both are same then it is palindrome. We use the following logic to see if a number is palindrome:

temp = n;

while( temp != 0 )

{

    reverse = reverse * 10;

    reverse = reverse + temp%10;

    temp = temp/10;

}

 

if ( n == reverse )

printf("%d is a palindrome number.\n", n);

else

printf("%d is not a palindrome number.\n", n);

 

Please see the video for more details

Was it useful?  



Following is the program for finding out if a string is palindrome or not . 

#include <stdio.h>

#include <string.h>

 

int main()

{

   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome\n");

   gets(a);

   strcpy(b,a);

   strrev(b);

   if(strcmp(a,b)==0)

      printf("Entered string is a palindrome.\n");

   else

      printf("Entered string is not a palindrome.\n");

  return0;

}

 

please find attached the program for finding a number is palindrome or not.


Was it useful?  




Please login to reply to this problem.