Palindrome checker - Madam Im Adam not working
Apr 25, 2016 at 9:09pm UTC
I have a program that checks if a given string is a palindrome. I'm supposed to acknowledge that there may be punctuation, and have the program act accordingly.
However, my program fails when I input "Madam Im Adam", though "madamimadam" works. Here's my palindrome function code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
bool isPalindrome(string wordPhraseOrSentence)
{
bool match = true ;
int beginningCounter = 0;
int endingCounter = wordPhraseOrSentence.length() - 1;
while (match)
{
char char1 = wordPhraseOrSentence.substr(beginningCounter,1).c_str()[0];
char char2 = wordPhraseOrSentence.substr(endingCounter,1).c_str()[0];
if (shouldIgnoreChar(char1))
{
beginningCounter += 1;
}
if (shouldIgnoreChar(char2))
{
endingCounter -= 1;
}
char1 = tolower(char1);
char2 = tolower(char2);
match = char1 == char2;
if (beginningCounter == endingCounter)
{
// Done.
break ;
}
beginningCounter += 1;
endingCounter -= 1;
}
return match;
}
Using the debugger, I found that after 5 iterations of the loop, char2 appears to be empty.
Apr 25, 2016 at 10:39pm UTC
Does your shouldIgnoreChar
function ignore spaces?
Apr 25, 2016 at 11:12pm UTC
Theoretically, yes. I don't have access to the code presently, but the method has an array of characters to be ignored, which includes ' '.
Topic archived. No new replies allowed.