Signup/Sign In

C Program To Check If A Given String Is Palindrome

Learn C language tutorial

What is Palindrome?

  • The Word should sound the same in reverse direction also, then the word is considered as Palindrome.
  • Get the input from the user and store it in the array,
  • Reverse the given string and store it in another array,
  • After storing the string in the reverse array, compare the source array and reversed array,
  • Compare the two strings, If the given string is similar print "The Given String Is Palindrome",
  • If the given string is not similar, print "The Given String Is Not Palindrome".

C Program To Check Whether The Given String Is Palindrome:

#include <stdio.h>
#include <string.h>
 
void main()
{
    char string[50], reverse[40] = {'\0'};
    int  x, length = 0, y = 0;
 
    fflush(stdin);
    printf("Enter The String To Find Whether The Given String Is Palindrome: \n");
    gets(string);
    for (x = 0; string[x] != '\0'; x++)
    {
        length++;
    }
    for (x = length - 1; x >= 0; x--)
    {
       reverse[length - x - 1] = string[x];
    }
    for (x = 0; x < length; x++)
    {
        if (reverse[x] == string[x])
            y = 1;
        else
            y = 0;
    }
    if (y == 1)
        printf("The Given String %s is a palindrome \n", string);
    else
        printf("The Given String %s is not a palindrome \n", string);
}

Output:

Case 1: If The Given String Is Palindrome,

pali

Case 2: If The Given String Is Not Palindrome,

Not