Tuesday, November 22, 2011

C Program to Check String is Palindrome or Not

It is a sample C language example. It is a string palindrome program which accept a string from user.
It checks that accepted string is a palindrome string or not. If string or string reverse is as same as then this string is a palindrome string. It means read string same in both forward or backward direction.

Aim - C program to check whether the accepted string is palindrome or not.

Program Source Code
//C Program to check accepted string is palindrome or not
int isPallindrome(char *);
void main()
  int a;
  char str1[30];
  printf("\n******String Palindrome******\n");
  printf("Enter a string: ");
  gets(str1); // accept string from user
  a=isPallindrome(str1);  /*function calls, accept string as argument and return flag value*/

   printf("\nstring: %s is a palindrome string",str1);
   printf("\nstring: %s is not a palindrome string",str1);


int isPallindrome(char *str2)
   int i,j,flag=1;

   for(i=0,j=strlen(str2)-1; i<j; i++, j--)
    if(str2[i]!=str2[j]) /*starts checking from ends of string to middle*/
    flag=0; /*if reverse of string is not same than return 0*/


******String Palindrome******
Enter a string: MADAM
string: MADAM is a palindrome string

******String Palindrome******
Enter a string: HELLO
string: HELLO is not a palindrome string

No comments:

Post a Comment