C program to check if a given string is palindrome or not

Finding if a given string is palindrome or not?

In this article we will learn how to code a C program to find if the given string is palindrome or not. A palindrome string is one that reads the same forward as well as backward. Strings like ‘redivider’, ‘deified’, ‘level’, ‘mam’ are examples of palindrome string. We need to find whether the first and last, second and second last, and so in the string are equal or not in order to find if the given string is palindrome or not.

C program to check if a string is palindrome or not

Algorithm:

  • Initialize the variable.
  • Accept the input.
  • Initialize for loop.
  • Check if string is palindrome or not.
  • Terminate for loop.
  • Print result.

C programming code to check if a given string is palindrome or not

Competitive Coding Techniques
#include <stdio.h>
#include <string.h>

int main()
{
    //Initializing variable.
    char str[100];  
    int i,length=0,flag=0;

    //Accepting input.
    printf("Enter  the string : ");
    gets(str);
    length=strlen(str);

    //Initializing for loop.
    for(i=0;i<length/2;i++)  
    {
      //Checking if string is palindrome or not.
      if(str[i]==str[length-i-1])
      flag++;

    }
      //Printing result.
      if(flag==i)
 	   printf("String entered is palindrome");
      else
           printf("String entered is not palindrome");

      return 0;
}
Output 1:
Enter the string : PREPINSTA
String entered is not palindrome

Output 2:
Enter the string : LEVEL
String entered is palindrome