A string is said to be palindrome if it remains unchanged after reversing it.  For Example 4646, aeoaeo, mom, dad, and nana are some examples of palindrome strings.
Below is the source code of the program in C to check if a given string is Palindrome. This program is successfully compiled, and the output is also given below.

Check Palindrome string using C

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

 int main () 
{
  
char string[50];
  
 
printf ("Enter a String: ");
  
scanf ("%s", string);
  
 
int count;
  
count = strlen (string) - 1;
  
 
int i = 0;
  
int palindrome = 1;
  
while (i <= count)
    
    {
      
if (string[i] != string[count])
	
	{
	  
palindrome = 0;
	  
break;
	
}
      
 
i++;
      
count--;
    
}

if (palindrome == 1)
    
    {
      
printf ("%s is palindrome", string);
    
}
  else  
    {    
printf ("%s is not palindrome", string);  
}
  
return 0;

}

Output

Palindrome String in C

Program Explanation

C program also starts execution from the main method. First of all, we declared an array string of type char to read the value from the user. A count variable is declared to count the number of characters in the string array. Strlen is a built-in function in C which counts the number of characters in a string.

While loop used to compare the string, inside for loop ‘if’ statement is used, if the given string 1st character is not equal to the last one then ‘if’ part will be executed and palindrome variable is initialized to 0.

After the while loop, an if-else statement is used. If the palindrome variable is equal to 1, then the entered string is palindrome else it is not a palindrome.

Related Articles

Last modified: May 5, 2019