C program to check whether the given string is palindrome or not.

Posted by Mangesh on March 14, 2018

/* check given string is palindrome or not. */

Description :

A string is a palindrome if the string is equal to its reversed, for example, "madam" is a palindrome as reverse of "madam" is "madam" whereas "program" is not a palindrome as its reverse is “margorp”. Some other examples of palindrome strings are "nitin", "mayalayam", "abcba".

Logic :

Instead of reversing the string and checking the result we will just check whether first letter is equal to last letter or not, second letter is equal to second last letter or not, and so on. If all n/2 check give result true then the string is palindrome else it is not.

Program :

int palindrome(char str[20])
   int i,length;
   length = strlen(str);
      if(str[i] != str[length-i-1])
          return 0;
   return 1;

void main()
   char str[20];
   int check=0;
   printf("Enter the word to check for palindrome:");
   check  = palindrome(str);
   if(check == 1)
      printf("%s is palindrome!",str);
     printf("%s is not palindrome!",str);

Output :

c program for palindrome` tested in turbo c 3.2

Skills Learned :

In-built function strlen().
If-Else statement.

Written with from Mangesh.

Related Post
1 C program for naive string matching algorithm.
2 C program to check whether the given string is palindrome or not.
3 C program to concatenate two string.
4 C program to check whether two strings are anagram or not.
5 C program to reverse the string.
6 C program to replace a character in a string.
7 C program to compare two string.
8 C program to compare two string using strcmp function.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.