Palindrome String Check


C Program to check whether a given string is a palindrome or not.


#include  <stdio.h>
#include  <conio.h>

void main()
{
  char  str[50] ;
  int i , j , length , flag ;

  clrscr() ;

  printf("Enter a string: ") ;
  gets(str) ;

  length=strlen(str) ;

  flag=1 ;   /* assuming that the string is a palindrome */
  i=0 ;
  j=length-1 ;
  while(i<length/2)
    if( tolower(str[i]) == tolower(str[j]) )
    {
       i++ ;
       j-- ;
    }
   else
   {
      flag=0 ;
      break ;
   }

  if(flag==1)
    printf("String %s is a palindrome" , str) ;
  else
    printf("String %s is not a palindrome" , str) ;

  getch() ;
}


Output 1:


Enter a string: Maam
String Maam is a palindrome

Output 2:


Enter a string: Madam
String Madam is a palindrome

Output 3:


Enter a string: apple
String apple is not a palindrome