I'm checking the palindrome using the stack. I want to receive input from the user, determine whether it is a palindrome, and output the result
I want to ignore it without using a method of removing spaces and special characters, but it doesn't go as I want.
The code is
//
int palindrome(char string[])
{
Stack s;
int ii;
initStack(&s);
for (ii = 0; string[ii] != '\0'; ii++)
{
if (string[ii] >= 'A' && string[ii] <= 'Z')
string[ii] += 32;
if (string[ii] >= 'a' && string[ii] <= 'z')
{
push(&s, string[ii]);
}
}
//printf("%s\n", string);
for (ii = 0; string[ii] != 0; ii++)
{
if (string[ii] != pop(&s))
{
return 0;
}
}
return 1;
}
int main()
{
char string[MAX_STACK_SIZE];
printf("Enter a String\n");
gets_s(string);
if (palindrome(string))
printf("palindrome!\n");
else
printf("X\n");
return 0;
}
In this part, the range between 'a' and 'z' was specified, and I wonder why special characters and spaces are not excluded and pushed into the stack.
for (ii = 0; string[ii] != '\0'; ii++)
{
if (string[ii] >= 'A' && string[ii] <= 'Z')
string[ii] += 32;
if (string[ii] >= 'a' && string[ii] <= 'z')
{
push(&s, string[ii]);
}
}