Recursive palindrome

Hi, I have a boolean function that recursively determines if something is a pali(assuming there are no spaces, special chars, etc), but I have a warning saying control reaches end of non-void function, but I can't seem to remove the error.
Can someone take a look?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool ispalindrome(string s){
    
    if (s.length() <= 1){
    
    return true;
    
        
    }

    if (s[0] == s[s.length()-1]){
    
    return ispalindrome(s.substr(1, s.length()-2));
    return false;
    
        
    }
    
}


(this is practice for a final exam. You won't be helping me cheat.)
When do you think line 13 will happen?
>> salem

When something is not a pali and it doesn't meet the s[0] == s[s.length()-1)] criteria??
then you are misreading your own code.
the if statement wraps BOTH return statements... lookit the brackets.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

bool ispalindrome(string s){
    
    if (s.length() <= 1){
    
    return true;
            
    }

    if (s[0] == s[s.length()-1]){
    
    return ispalindrome(s.substr(1, s.length()-2));

    }
    
    else{ //good grief
        
    return false;
    
    }
        
}


another facepalm

I'm a big stressball before a test. It's showing up in these stupid mistakes.

Thanks yall.

Last edited on
Topic archived. No new replies allowed.