Hello programmers,
My assignment is to create a function that checks whether or not the user inputted string is a palindrome.
I think I made a mistake in the palindrome function because it seems to be unable to check inputs with spaces in it. The error most likely lies in the section where I attempt to use the isalpha function.
Here is my code, any help is very much appreciated!
Code:/*------------------Main Functions------------------ Purpose: Prompts user to input string Returns: Singal to the OS --------------------------------------------------*/ int palindrome (char *string); int main(void) { char input[81]; char *stop = "done"; printf("Enter a string: "); gets(input); while( strcmp ( input, stop ) != 0) { if( palindrome ( input ) ) { printf("\nIt is a palindrome\n"); } else { printf("\nIt is not a palindrome\n"); } printf("\nEnter a string: "); gets(input); } printf("\n***** Program Terminated *****\n"); } /*------------------Palindrome Functions------------------ Purpose: Checks whether input is a palindrome Returns: TRUE or FALSE --------------------------------------------------*/ int palindrome (char *string) { char *string_end = string + strlen(string) - 1; while(TRUE) { while( isalpha( *string ) ) { string ++; } while( isalpha( *string_end) ) { string--; } if( string_end <= string) { return TRUE; } if( tolower(*string) != tolower(*string_end)) { return FALSE; } string_end--; string++; } }