Check the given string is Palindronme

Get a String and reverse the characters of the string. And check whether reverse order and original order is same.

Sample Input 1:

madam

Sample Output 1:

Palindrome

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
#include<stdio.h>
#include<string.h>
int main()
{
	char str[50],str1[50],temp;
	int i = 0;
	int j;
	fgets(str,50,stdin);
	j = strlen(str) - 1;
	while(i<=j)
	{
		str1[j]=str[i];
		str1[i]=str[j];
		i++;
		j--;
	}
	printf("\n%s\n",str1);
	i=0;
	while(str[i]!='\0' && str[i]!='\n')
	{
		if(str[i]!=str1[i])
		{
			printf("Not palindromne");
			return 0;
		}
		i++;
	}
	printf("Palindronme");
	return 0;
}
			
				
			

Program Explanation

initialize i to first location of string and j to last location of string using i=0 j=n-1 move the characters of str in location i and j to str1, then increment i by 1 and decrement j by 1.

str1[j]=str[i] str1[i]=str[j] repeat the above step till i is less than j Compare str and str1, if the both are same then print "Palindromne". else "Not Palindromne".

Comments