I am writing a code for checking whether the string is palindrome or not.I want to neglect spaces and punctuation marks or any other non alphabetic character. which basically means "madam 'I Imadam" should be a palindrome as well according to my code. But not getting the appropriate result.
#include <stdio.h>
#include <stdlib.h>
void chkpalindrome(char []);
int main()
{
char s[50];
gets(s);
chkpalindrome(s);
return 0;
}
void chkpalindrome(char a[50])
{
int i=0;
int j=0;
int flag=1;
while(a[j+1]!='\0') //so that 'j' should point to the last index of string
{
j=j+1;
}
while((i!=j)&&(i!=(j+1)))
{
if((a[i]<'A')||('Z'<a[i]<'a')||(a[i]>'z'))
{
i=i+1;
}
else if((a[j]<'A')||('Z'<a[j]<'a')||(a[j]>'z'))
{
j=j-1;
}
else
{
if(a[i]!=a[j])
{
flag=0;
break;
}
else
{
i=i+1;
j=j-1;
}
}
}
if(flag==1)
{
printf("IT IS A PALINDROME");
}
else
{
printf("IT IS NOT A PALINDROME");
}
}
Expected results-madam I' Imadam -IT IS A PALINDROME evee-it is not a palindrome But actual results are coming out to be every string as a palindrome
A form 90<a[i]<97
is interpreted as (90<a[i])<97
, so of course this is not what you expected
Must be (90<a[i]) && (a[i]<97)
You have several times that error
And as it was said in remarks use char like 'a' rather than the code
rather than to do
while(a[j+1]!='\0') //so that 'j' should point to the last index of string
{
j=j+1;
}
I encourage you to use strlen