C Program to Check Palindrome String

Here we will write a C program to check the palindrome string. For this we can use strlen() function defined under <string.h>. The same thing can be done without using strlen() function.

If the reverse of the string is the same string then the string is called palindrome. Some examples of palindromic words are redivider, noon, civic, radar, level, rotor, kayak, reviver, racecar, redder, madam, and refer. The palindrome number is also based on the palindrome string. The reverse of a number is equal to the same number is called palindrome number.

Without using String Manipulation Library Functions

#include<stdio.h>
int main()
{
   char s1[100], s2[100];
   int i, j, flag=1;
   printf("Enter a string: ");
   scanf("%[^\n]",ch);

   //find String length
   for(i=0; s1[i]!='\0';i++);

   //Copying string in reverse order
   for(i--,j=0; i>=0; i--, j++)
   {
     s2[j]=s1[i];
   }
   s2[j]='\0';

   //String comparision
   for(i=0; s1[i]!='\0';i++)
   {
     if(s1[i]!=s2[i])
     {
       flag=0;
       break;
     }
   }

   if(flag==1)
   printf("String is Palindrome.");
   else printf("String is not Palindrome.");

   return 0;
 }

Output:-

Enter a string: madam
String is Palindrome.

Enter a string: 12321
String is Palindrome.

Enter a string: Know Program
String is not Palindrome.

In the above program, we take a string as input from the user. After that, we find the length of this string and this string is copying into another string in reverse order. Now, both strings are compared. If both strings are equal then string is palindrome otherwise it is not a palindrome number.

Using the above program we can also find that the number is palindrome number or not.

C Program to Check Palindrome String using String manipulation Library functions

#include<stdio.h>
#include<string.h>
int main()
{
   char s1[100], s2[100];
   printf("Enter a string: ");
   scanf("%[^\n]",ch);

   strcpy(s2,s1);//Copy the string
   strrev(s2);//reverse the string

   if(strcmp(s1,s2)==0)
   printf("Palindrome.");
   else printf("Not Palindrome.");

   return 0;
}

Output:-

Enter a string: madam
Palindrome.

Enter a string: Madam
Not Palindrome.

There is one case we know that “Madam” is also a palindrome, but our program shows it’s not a palindrome due to case sensitivity.
Starting M is in the capital but the last m is in the small case. To ignore this case sensitivity we can use stricmp() function in place of strcmp() function.

if(stricmp(s1,s2)==0)

Enter a string: Malayalam
Palindrome.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

More C programming Examples on String

1 thought on “C Program to Check Palindrome String”

  1. Hey! I’m at work surfing around your blog from my new iphone!Just wanted to say I love reading your blog and look forward to all yourposts! Carry on the great work!

Leave a Comment

Your email address will not be published. Required fields are marked *