I have a palindrome function that returns true if a word can be spelled the same way backwards as it is forward. The function works but I need to make it so it is not case sensitive. So, aBbA should return true. I have tried using the toupper function to correct this and it is not working. I put the toupper() in a cout statement to observe its value and it looks like its giving me an address instead of the char values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool isPalindrome(constchar* inString)
{
char* start = (char*)inString; // assign 1st address of inString to start and truncate const
char* end = start + strlen(inString) - 1;// assign 1st address + last address to end
while (start < end) { // ist address < last address
cout << toupper(*inString);
if (*start != *end) // 1st char != last char as they increment toward each other.
returnfalse;
start++; // increment to next address
end--; // decrement to next address
}
returntrue;
}
I have tried using an additional while loop before the first one and this didn't work either.
1 2 3 4 5
while (*inString != NULL) {
toupper(*inString);
inString++;
}
I figured it out.
1 2 3 4
while (*start != NULL) {
*start = toupper(*start);
start++;
}