Thursday, 16 March 2017

C Program to Check if a String is palindrome or not

A palindrome word or string is a word that reads the same backward or forward. C program to check if a string is palindrome or not is given below. Approach is simple, first we reverse the original string and then we will compare both the string character by character. If both strings are same character by character then the given string is palindrome otherwise not.

C Program to check if a string is palindrome or not
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
   char original[MAX],reverse_of_original[MAX];
   int i,j,flag=0;

   /**taking input **/

   printf("\nEnter the String: \n");
   scanf("%s",original);
      
      /**Reversing the Original String **/

      for(i=strlen(original)-1;i>=0;i--)
      {
            reverse_of_original[strlen(original)-i-1]=original[i];
      }

      /* compairing both strings */
      
      for(i=0;i<strlen(original);i++)
      {
            if(original[i]!=reverse_of_original[i])
            {
                flag=1;
                break;
            }
      }
      if(flag==1)
      {
            printf("\nString is not palindrome");
      }
      else
      {
            printf("\nString is a palindrome");
      }
      return 0;
}


OUTPUT:
 


EmoticonEmoticon