I would like to have some advice on how I can make my palindrome function read an empty c string like char blank[] = ""; as a palindrome
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool isPalindrome(/* inout */ constchar* inString)
{
//Initializes variables
int i = 0, j = strlen(inString) -1;
//For loop used to go and see if the string is the same read backwards
for(i = 0; i < j; i++)
{
//Makes the if statement non case sensitive
if(toupper(inString[i]) != toupper(inString[j]))
{
returnfalse;
}
j--;
}
}
It seems to me you're missing "return true" at the end of the function.
Don't you see a message like this when you compile it?
1 2 3 4 5
$ g++ -Wall foo.cpp
foo.cpp: In function ‘bool isPalindrome(constchar*)’:
foo.cpp:22:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Chances are, you're falling off the end of the function with zero when you have an empty string, and some other number when you have a palindrome (but anything != 0 counts as true).
if(isPalindrome(blank))
cout << "The string " << blank << " is a palindrome" << endl;
else
cout << "The string " << blank << " is not a palindrome" << endl;