Hi everyone, I have a question on the logic of the attached code. In the code there is four "isapha" loops. The first two are nested inside of while loop one and the next two are nested inside of while loop two. My question is why is there a need of two sets. I know what "isalpha" does.
Code:
/* preprocessor directives */
#include <stdio.h>
#include <ctype.h>
/* Function declaration and function. */
int palindrome (char* string)
{
char *strg_beg, *strg_end;
strg_beg = strg_end = string;
while (*strg_end != '\0')
strg_end++;
strg_end-=2;
while (!isalpha(*strg_beg))
strg_beg++;
while (!isalpha (*strg_end))
strg_end--;
while (tolower(*strg_beg) == tolower(*strg_end) && strg_beg <= strg_end)
{
strg_beg++;
strg_end--;
while (!isalpha (*strg_beg))
strg_beg++;
while (!isalpha (*strg_end))
strg_end--;
}
while (strg_beg < strg_end)
{
if (*strg_beg != *strg_end)
return 0;
strg_beg++;
strg_end--;
}
return 1;
}
/* Main function */
int main (void)
{
char input[80] = "";
printf ("Enter a string: ");
fgets (input, 80, stdin);
input[79] = 0;
if (palindrome (input))
{
printf("\nThe string, %s\nis a Palindrome.\n", input);
}
else
{
printf("\nThe string, %s\nis NOT a Palindrome.\n", input);
}
return 0;
}