C Program to find the length of a string and check for palindrome

What is palindrome string?

Palindrome Means any string or number, which is the same when we Reverse it. For instance, a String "Malayalam" is a palindrome String because it peruses the same when we read it from the front or from backward. Another example is "madam", "refer", "level". Correspondingly "English" is likewise a String, yet it is not Palindrome because it is not the same when understanding it from rearward. Please refer to the previous program no '28' to get more about Palindrome.

How palindrome check with string length calculation is implemented in C?

In this C program, we need to find the length of the String. A String in a C program is a statement or an arrangement of characters closures by space. Also, we need to check the String is Palindrome or not. First, we have to calculate the length of the String by counting the number of characters in the String. It is just by increment the Counter variable with one on reaching each Character.

So finally, we get the number of characters in the String. Then we will find out the Reverse of the String by using a For loop. After that, we will check whether the 'revString' and the given 'string' are equal or not. If the Strings are similar, display the String as Palindrome; otherwise, we will show the String is not a palindrome.


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

STEP 2: Declare the Character Array 'string' and 'revString.'

STEP 3: Declare the integer variables i,length,flag.

STEP 4: Set length=0,flag=0.

STEP 5: Clear the output buffer by calling the function fflush(stdin).

STEP 6: Read the String using the gets() function.

STEP 7: By using a for loop with the condition string1[i] != '\0' increment length by 1.For getting the length of the String.

STEP 8: Then display the String and length of the String as String and length.

STEP 9: Assign i=length-1.

STEP 10: Using another for loop with the condition i >= 0, do step 11.

STEP 11: Assign revString[length-i-1] = string[i].

STEP 12: Decrement 'i' by 1 and repeat step 10.

STEP 13: Assign i=0.

STEP 14: By using another for loop with the condition i < length do step 15.

STEP 15: check if revString[i] == string[i] then set Flag=1 else set Flag=0.

STEP 16: Check if Flag == 1 then display the String is Palindrome else do step 17.

STEP 17: Display the String is not a Palindrome.

C Source Code

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

void main() {
  char string[25], revString[25] = {
  int i, length = 0, flag = 0;
  printf("Enter a string\n");
  for (i = 0; string[i] != '\0'; i++) /*keep going through each */ {
    /*character of the string */
    length++; /*till its end */
  printf("The length of the string \'%s\' = %d\n", string, length);
  for (i = length - 1; i >= 0; i--) {
    revString[length - i - 1] = string[i];
  /*Compare the input string and its reverse. if both are same then we can tell it is palindrome, else not an palindrome */
  for (i = 0; i < length; i++) {
    if (revString[i] == string[i])
      flag = 1;
      flag = 0;
  if (flag == 1)
    printf("%s is a palindrome\n", string);
    printf("%s is not a palindrome\n", string);


Enter a string

The length of the string 'madam' = 5
madam is a palindrome


Enter a string
The length of the string 'good' = 4
good is not a palindrome