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

 Write a C program to check whether a given string is a palindrome or not?

A string could be a 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.

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;
}

ANALYSIS

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;
}
}
len = 3
Flag = 0

str[] = wow

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

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

In this string palindrome program, we are using the built-in functions strcpy, strrev, 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 the 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;
}


What do you think?

I hope, now you have a better understanding of palindrome Comments and suggestions regarding this article are welcome.


Comments

  1. Doing good job dear it definitely helps to students

    ReplyDelete
  2. thank you so much hope you will go for more in details

    ReplyDelete

Post a Comment

Popular posts from this blog

What is a string ? Write a function in C for string concatenation. Without the use of inbuilt string function?

What is the scope of a Variable? Explain the difference between a global and local variables with example programs.

What is a pointer ? Write a C program using pointer to print the name and price of the items sold in a retail shop on a specific date.