What does program solve:
Find palindrome from user input.
What issue to tweak:
How to make code accept sentence like Rise to vote sir! as Palindrome if we disregard punctuation and blank spaces. If you read it backwards it will be the same!
Things that I have tried: I tried to just save characters a-z, A-Z, and 0-9 into character-type array then proceed with the logic on it. But to no avail.
I wish if someone could suggest where to tweak the following C snippet, in a way it can handle phrase or sentence to not include spaces, punctuation, and special characters within the computation of the code logic.
/* Search for a palindrome */
#include <stdio.h>
#include <ctype.h>
#define EOL '\n'
#define TRUE 1
#define FALSE 0
int main()
{
int tag, count, countback, flag, loop = TRUE;
char letter[80];
/* main loop */
while (loop)
{
/* anticipated palindrome */
flag = TRUE;
/* read in the text */
printf("\nPlease enter a word, phrase, or sentence below:\n");
for (count = 0; (letter[count] = getchar()) != EOL; ++count)
;
/* test for end of program keyword END */
if ((toupper(letter[0]) == 'E') && \
(toupper(letter[1]) == 'N') && \
(toupper(letter[2]) == 'D'))
break;
tag = count - 1;
/* carry out the search */
for ((count = 0, countback = tag); count <= tag / 2; (++count, --countback))
{
if (letter[count] != letter[countback])
{
flag = FALSE;
break;
}
}
/* display message */
for (count = 0; count <= tag; ++count)
putchar(letter[count]);
if (flag)
printf(" --> IS a Palindrome!\n\n");
else
printf(" --> is NOT a Palindrome.\n\n");
} /* end of main loop */
return 0;
}
In the loop, add sub-loops such that while
!isalnum(letter[count])and!isalnum(letter[countback])are true; incrementcountand decrementcountbackrespectively. e.g.:However doing that breaks your outer loop because you can no longer make the assumption that the centre of the string is the centre of the possible palindrome. It is better in any case to simply test that
count > countback. e.g:and because you have then modified the control variables in the loop, you need to add a check in the "not palindrome" test, and you also want to ignore case:
That test can in fact be simplified:
To avoid undefined behaviour of the ctype.h functions, the
letterarray should have typeunsigned charalso.That is the minimal "tweak" as you asked. More of a comment than part of the answer, but I would suggest some "style" improvements too on structure, commenting, naming, scope and I/O; my re-implementation of your code for what it is worth :
This also allows strings such as
"end Ådne"and"end of the world"to be tested without terminating as yours would.Further it is always a good idea to separate processing from I/O. To that end you might create a function:
then in the main body you would simply have:
or even: