palindrome function not working

Hey guys,
I'm currently working on a function which checks whether the string argument is a palindrome (word which remains the same if it's read backwards) or not.
I wanted to achieve this by using a recursive call.
Long story short, here's my 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
#include <string>
#include <iostream>
using namespace std;

bool palindrome(string word) {
    int i = 0;
    int j = word.size()-1;
    if (i >= j) return true; // end of recursion
    if (word[i] != word[j])
        return false;
    word.erase(word.begin());
    word.erase(word.end()-1);
    palindrome(word);    // recursive call
}

int main() {
    bool b = palindrome("ada");
    if (b)
        cout << "Palindrom!\n";
    else
        cout << "Not a palindrom\n";

    return 0;
}


It always gets me "Not a palindrom", but can't figure out the mistake.
Thanks in advance :)
Last edited on
you forgot to return palindrome(word);!
Wow it's quite obvious now.
Thanks for the quick reply
Topic archived. No new replies allowed.