Problem: Write a program in C to check whether a given string is a palindrome or not.

Example:

A palindrome string is a string which when reversed is equal to the original string. For example mom, noon, malayalam, etc.

The best method to check palindrome string is to compare the first half of the string with the reverse of its second half.

check palindrome string

Steps to check palindrome number in C:

  1. Input a string.
  2. Find the length of the string.
  3. Loop from i=0 to i<length/2 with k=length-1.
    1. check if string[i] != string[k].
      1. If the condition is true, then the characters are not the same hence the given string is not a palindrome, so break out of the ‘for’ loop.
  4. Otherwise, the string is Palindrome.

Here is the implementation of the steps in C:

Output:

Palindrome string in C
Check whether string is palindrome o

To compare the first half of the string with its second half, we have used two index variable i and k.

After each iteration, we increment i and decrement k to keep comparing the successive characters of the first half with their corresponding characters in the second half.

If in any iteration the comparison fails (i.e. string[i] != string[k]), then the input string is not a palindrome.

It is important to note, that the above program to check palindrome string is case sensitive. If the user inputs “Mom” then the output will be “Not Palindrome”. In order to make it case sensitive, lower the case of the string after the user input.

Leave a Reply

one + sixteen =