C Program to Check the Given String is Palindrome

A string could be Palindrome string in C if it remained the same when we reversed it. Let us see how to write a C Program to Check the Given String is Palindrome using Built-in functions, and without using built-in functions.

C Program to Check the Given String is Palindrome Example 1

This program for string palindrome in c allows the user to enter a string (or character array), and a character value. Next, it will check whether the user-specified string is a palindrome string or not.

/* C Program to Check the given string is Palindrome or not */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100];
  	int i, len, flag;
  	flag = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	len = strlen(str);
  	 	   	
  	for(i = 0; i < len; i++)
	{
		if(str[i] != str[len - i - 1])
		{
			flag = 1;
			break;	
		} 
	}
	if(flag == 0)
	{
		printf("\n %s is a Palindrome String", str);
	}
	else
	{
		printf("\n %s is Not a Palindrome String", str);
	}	
	
  	return 0;
}
C Program to Check the given string is Palindrome or not 1

Here, we used For Loop to iterate every character in a String, and check the palindrome condition

for(i = 0; i < len; i++)
{
	if(str[i] != str[len - i - 1])
	{
		flag = 1;
		break;	
	} 
}

str[] = wow
len = 3
Flag = 0

For Loop – First Iteration: for(i = 0; i < len ; i++)
The condition is True because 0 < 3.

Within the For Loop we used If Statement to check whether the str[1] is not equal to str[len – i – 1]
if(str[i] != str[len – i – 1]) => if(str[0] != str[3 – 0 – 1])
if(w != w) – Condition is False. So, Flag is still 0

Second Iteration: for(i = 1; 1 < 3 ; 1++)
if(str[i] != str[len – i – 1]) => if(str[1] != str[3 – 1 – 1])
if(o != o) – Condition is False. So, Flag is still 0

Third Iteration: for(i = 2; 2 < 3 ; 2++)
if(str[i] != str[len – i – 1]) => if(str[2] != str[3 – 2 – 1])
if(w != w) – Condition is False. So, Flag is still 0

Next, we used If Else Statement to check the Flag value is 0 or not. Remember, If the condition in any of the loop is True, then Flag value will become one, and the string will not be palindrome string in C

Let me check another string

 Please Enter any String : tutorial

 tutorial is Not a Palindrome String

C Program to Check the Given String is Palindrome Example 2

This string palindrome in c program is the same as above, but we changed the logic a bit.

/* C Program to Check the given string is Palindrome or not */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100];
  	int i, len, stratIndex, endIndex;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	len = strlen(str);
  	endIndex = len - 1;
	    	 	   	
  	for(i = 0; i <= endIndex; i++)
	{
		if(str[i] != str[endIndex])
		{
			break;	
		} 
		endIndex--;		
	}
	if(i >= endIndex)
	{
		printf("\n %s is a Palindrome String", str);
	}
	else
	{
		printf("\n %s is Not a Palindrome String", str);
	}	
	
  	return 0;
}

C palindrome string output


 Please Enter any String : aakkaa

 aakkaa is a Palindrome String

C Program to Check the Given String is Palindrome Example 3

In this C program for string palindrome, we are using the traditional approach. First, we are reversing the string, and then we are comparing the reversed string with the original string. I suggest you to refer Reverse a String article to understand the steps involved in string reverse.

/* C Program to Check the given string is Palindrome or not */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], revstr[100];
  	int i, j, len, flag;
  	flag = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	len = strlen(str);
  	j = 0;

  	// Reverse a String 	   	
  	for(i = len - 1; i >= 0; i--)
	{
		revstr[j++] = str[i];
	}
	revstr[i] = '\0';

	// Comparing Reverse String and Original String
	for(i = 0; i < len; i++)
	{
		if(str[i] != revstr[i])
		{
			flag = 1;
			break;	
		} 
	}
	if(flag == 0)
	{
		printf("\n %s is a Palindrome String", str);
	}
	else
	{
		printf("\n %s is Not a Palindrome String", str);
	}	
	
  	return 0;
}
 Please Enter any String :  madam

 madam is a Palindrome String

Program to Check the Given String is Palindrome Example 4

This program for string palindrome in c is the same as the second example. Still, this time, we are using the Functions concept to separate the logic.

/* C Program to Check the given string is Palindrome or not */
 
#include <stdio.h>
#include <string.h>

void isPalindrome(char str[]);
 
int main()
{
  	char str[100];
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	isPalindrome(str);	
	
  	return 0;
}

void isPalindrome(char str[])
{
	int i = 0;
	int len = strlen(str) - 1;
	
	while (len > i)
	{
		if(str[i++] != str[len--])
		{
			printf("\n %s is Not a Palindrome String", str);
			return;
		}
	}
	printf("\n %s is a Palindrome String", str);
}
 Please Enter any String :  aaaabbbaaaa

 aaaabbbaaaa is a Palindrome String

Program to Check the Given String is Palindrome using  Built-in Functions

In this string palindrome program, we are using the built-in functions strcpystrrev, and strcmp. First, the C Programming strcpy copies the user given string to a new string. Next, we used the strive to reverse the string. And finally, we used strcmp to compare the original string with reverse one.

/* C Program to Check the given string is Palindrome or not */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], revstr[100];
  	int flag;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);

  	// To Copy the string to revstr
  	strcpy(revstr, str);

  	strrev(revstr); // reverse the string

  	// Comparing string
	flag = strcmp(str, revstr); 

	if(flag == 0)
	{
		printf("\n %s is a Palindrome String", str);
	}
	else
	{
		printf("\n %s is Not a Palindrome String", str);
	}	
	
  	return 0;
}
 Please Enter any String :  rar

 rar is a Palindrome String