I've written this function isPalindrome that is meant to take a string input from a separate function, and return 1 if it is a palindrome, and 0 if it is not. The input would be any characters, and may have capitals in it, and the function is meant to sort through these and purely check if it is a palindrome based on the alphabetic characters.
I've been on this for a while and I can't figure out what's going wrong, the whole function is below, but I can't make sense of the output it's giving me, sometimes it completely skips the last else statement, and stops the loop, and I've no idea why. When two non alphabetic characters are entered in a row, the variable a or b does not increment twice, rather it sends it to the last else statement and returns an incorrect value.
I'm trying to write this function without copying any information into separate arrays as well.
int isPalindrome(char s[])
{
int logic;
int a = 0;
int b = 0;
int num = 0;
int count = 0;
while ( s[b]!='\0' )
{
if ( isalpha(s[b]) != 0 )
{
num++;
}
b++;
}
b = b - 1;
printf("The number of characters is: %d\n", b);
printf("The number of alpha characters is: %d\n", num);
while ( count < num/2 )
{
if ( !isalpha(s[a]) || s[a] == ' ')
{
a++;
}
else
{
count++;
}
if ( !isalpha(s[b]) || s[b] == ' ')
{
b--;
}
if ( toupper(s[a]) == toupper(s[b]) )
{
printf("s[%d]: %c | s[%d]: %c\n", a, toupper(s[a]), b, toupper(s[b]));
a++;
b--;
if ( a == b )
{
logic = 1;
}
}
else
{
logic = 0;
break;
}
}
return logic;
}