C Program to check a string is Palindrome or not

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is a palindrome string?

A string in a c program is a word or a sequence of characters that ends with a null character. Here we need to find out if the string is palindrome or not. A palindrome is any string or number, which is the same when we reverse it.

For example, the string "Malayalam" is a palindrome because it reads the same when we read it from the front or backward. Similarly, "English" is also a string, but it is not a palindrome because it is not the same when reading it from backward.

How to check palindrome string in C?

The logic of this c program is, first declare two character arrays named string and revstring.The string array contains the input string and uses revstring to store the reverse of the given string. Then declare and initialize the variables i, length, and flag to zero. 'length' is the variable for storing the length of the string.

After that, we have to find out the length of the given string using a for loop with the condition string[i]!= '\0'. It means that it does not reach the end of the string. By default, the last character of the string is '\0'.

After calculating the length of the given string, the next step is to calculate the reverse of the string. The logic for finding the reverse of a string is to place the last character of the given string to the first place of the revstring array by using a for loop.
Now the revstring array contains the reverse of the string. After that, we have to check whether the string and revstring are equal or not. If the strings are identical, 'set flag=1' otherwise 'set flag=0'.Then check if 'flag=1' if so, display the string is a palindrome; otherwise, display the string is not a palindrome.

ALGORITHM

STEP 1:Include the Header files to use the built-in functions in the C program.

STEP 2: Include the Header string.h for handling an array of characters.

STEP 3: Declare 2 character Arrays string and revString. The Array String is to hold the input String, and the revString is to keep the reverse of the String.

STEP 4: Declare the Integer variables I, length, flag, and set length=0, and flag=0.

STEP 5: Accept the String by using printf and gets() function.

STEP 6: Find out the length of the given string by using a for loop with the condition string[i]!= '\0'.

STEP 7: Then place the last character of the given string to the first character of the revString using for loop.

STEP 8: After that, compare both the Strings using for loop.

STEP 9: If the String and revString are equal, then set flag=1, else set flag=0.

STEP 10: If flag=1, then display the String is a Palindrome.

STEP 11: If flag=0, then display the String is not a Palindrome.

C Source Code

                                          #include <stdio.h>
#include <string.h>

void main()
{
  char string[25], revString[25] = {'\0'};
  int i,length = 0, flag = 0;
  fflush(stdin);
  printf("Enter a string\n");
  gets(string);
  for (i = 0; string[i] != '\0'; i++) /*keep going through each */ {
    /*character of the string */
    length++; /*till its end */
  }
  for (i = length - 1; i >= 0; i--) {
    revString[length - i - 1] = string[i];
  }


   for (flag  = 1, i = 0; i < length; i++)
   {
      if (revString[i] != string[i])
      flag  = 0;
   }

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

   return 0;
}
   
                                      

OUTPUT

RUN 1
Enter a string
madam
madam is a palindrome

RUN 2

Enter a string
Madam
Madam is not a palindrome

RUN 3

Enter a string
good
good is not a palindrome