Remove character from string

I'm trying to remove characters from a string where the characters exist twice. For example:

tattletale
atletale
tletle
lele
ee

I've got the idea for it just having trouble on how to remove the letters from the string (I always have trouble with strings). How might I remove the characters I need? This is my code so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isEvenWord(string w)
{
        if(w == ""){
                return true;
        }
        if(w.find(w[0])){
                w.erase(w.find(w[0]),1);
                w.erase(0,1);
                isEvenWord(w);  
        }
        return false;
}

w.find(w[0]) would return 0, always.
What might I use to find if the string had another characters same as the first then?
 
size_t pos = w.find(w[0],1); // the second argument skips w[0] itself 


if pos == string::npos then it didn't find another occurrence of the character
if it did find one you can

1
2
w.erase(pos,1); // erase second occurrence
w.erase(0,1); // erase first occurrence 
This is what I have so far and it still doesn't work. It never returns true no matter what word I enter. (I don't know what it is about me and strings, but I have so much trouble with them. I know they are basically just an array of chars but I can NEVER get string functions to work the way I want. )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isEvenWord(string w)
{
        size_t pos = w.find(w[0],1);
        if(w == ""){
                return true;
        }   
        if(pos != string::npos){
                w.erase(pos,1);
                w.erase(0,1);
                isEvenWord(w);  
        }   
        return false;
}


This isn't really a problem with strings, it's a problem with recursion.

1
2
3
4
5
6
7
8
        if(pos != string::npos){
                w.erase(pos,1);
                w.erase(0,1);
                isEvenWord(w);    // here, you recursively call isEvenWord
                    // but you do not use its return value (ie, it is thrown away)
        }   
        return false;  // then you return false
}


Notice that your function will return false always unless 'w' is an empty string.

EDIT: To possibly clarify futher, you're doing this:

1
2
3
4
5
6
7
8
9
10
bool isEvenWord(string w)
{
        size_t pos = w.find(w[0],1);
        if(w == ""){  // do we have an empty string?
                return true;  // if yes, returntrue
        }   
        //  otherwise, do some more junk here

        return false;  // and return false
}


So the function is not returning true if the word is even, it's returning true if the word is empty.
Last edited on
Exactly. That's why I have the two calls to erase so that every call, if it finds two characters the same, it erases them. Then (if the word is even) it will eventually erase all the characters and I'll have an empty string and it will meet my base case and the word is even and it should return true. If the word is not even and there are no matching characters left to remove it returns false. At least, that is what I'm trying to get it to do.

**EDIT**

Wait..I think I see what you mean now.

Yeah, got it to work. Just needed to return the isEvenWord like Disch said. I feel like an idiot now. Thanks for the help!
Last edited on
Topic archived. No new replies allowed.