Monday, March 23, 2015

C program to check palindrome

Palindrome is a string,which when read in both forward and backward way in same.
Example:-madam,radar,rotavator

#include<stdio.h>
char string_palin(char str[]);

int main()
{
 char str[30];
 clrscr();
 printf("Enter string : ");
 scanf("%s",&str);

 if(string_palin(str))
    printf("Entered string Palindrome");
 else
    printf("Entered string not Palindrome");
    getch();
 return 0;
}

/*function for palindrome*/

char string_palin(char str[])
{
 int i,j;
 for(i=0; str[i]!=NULL; i++);
 for(j=0,i--; j<=i; )
 {
   if(str[i]==str[j])
   {
      i--;
      j++;
   }
  else
      break;
 }
 if(j>i)
    return(1);
 else
    return(0);
}

No comments: