C Programming

How to check if a string is a Palindrome

A String can be checked for palindrome using two while loops. One for counting the string length and second for checking the [i]th element of the substring with the original string to be compared with.
Related : Check for Palindrome in VB.Net

Language : C

[sourcecode language='c']
//Check if string is Palindrome
#include
#include

void main()
{
int i,count,flag,j;
char str[50];
flag=0;

printf(“Enter a string : “);
gets(str);

count=0;
i=0;
while(str[i]!=’\0′)
{ count++;
i++;
}
i=0;
j=count-1;;
while(i {
if(str[i]!=str[j])
{
flag=1;
break;
}

i++;
j–;
}
if(flag==0)
printf(“Palindrome! “);
else
printf(“Not Palindrome”);

}
[/sourcecode]



Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal