C Program to Check the Given String is Palindrome

A string could be Palindrome 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 using for loop

This program for string palindrome in c allows the user to enter a character array and a character value. Next, it will check whether the user-specified character array is a palindrome string 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 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 the 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, the 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, the 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, the Flag is still 0

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

Let me check another.

 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 program is the same as above, but we changed the logic a bit.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100];
  	int i, len, stratIndex, endIndex;
 
  	printf("\n Please Enter any Text :  ");
  	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", str);
	}	
	
  	return 0;
}

 Please Enter any Text : aakkaa

 aakkaa is a Palindrome String

C Program to Check the Given String is Palindrome using reverse

In this program, we are using the traditional approach. First, we reverse it, and then we compare the reversed string with the original. I suggest you refer to the Reverse article to understand the steps involved in string reverse.

#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 Text :  ");
  	gets(str);
  	
  	len = strlen(str);
  	j = 0;

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

	// Comparing Reverse and Original
	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 No", str);
	}	
	
  	return 0;
}
 Please Enter any Text :  madam

 madam is a Palindrome String

C Program to Check the Given String is Palindrome using functions

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

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

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

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

 aaaabbbaaaa is a Palindrome String

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

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

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], revstr[100];
  	int flag;
 
  	printf("\n Please Enter any Str :  ");
  	gets(str);

  	// Copy to revstr
  	strcpy(revstr, str);

  	strrev(revstr); // reverse

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

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

 rar is a Palindrome String