Need help with palindrome function

For my class I need to make my function able to read palindrome strings (Words that are spelled the same backwards ex: racecar) while ignoring punctuation and spaces Ive made a function that can read and print basic palindromes like racecar, but I don't know how to make it read a palindrome like ex: Was it a cat I saw?
Please help!
1
2
3
4
5
6
7
8
9
bool is_palindrome( string s )
{
    string rev = s;
    reverse(rev);//now string rev is reversed of s
    if(rev == s)
        return true;
    else
        return false;
is there a way I can take out the spaces and punctuation put into a string, Like turn Was it a cat I saw? To wasitacatisaw and have my current program detect it from there?
Last edited on
Yeah, quite easily but I'm probably going to do it a lot more complex than it needs to be XD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is_palindrome(std::string in){
    std::string st1;
//This for loop removes all puntuation
    for(int i=0; i<in.size(); i++){
        if(isalpha(in[i]))
            st1.push_back(in[i]);
    }
//This checks for palindrome
    for(int i=0, j=st1.size()-1; i<(in.size()/2); i++, j--){
        if(st[i] != st[j])
            return false;
    }
    return true;
}
Last edited on
Topic archived. No new replies allowed.