Advice to modify function so it can read empty string as a palindrome

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 */ const char* 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]))
        {
            return false;
        }
        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(const char*)’:
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).
I have it as an if function in main so it returns it as true
Like this

1
2
3
4
5
6

            if(isPalindrome(blank))
                cout << "The string " << blank << " is a palindrome" << endl;
            else
                cout << "The string " << blank << " is not a palindrome" << endl;
Last edited on
I added return true; and it worked as expected thanks !
Topic archived. No new replies allowed.