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.
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. )
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)
}
returnfalse; // 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?
returntrue; // if yes, returntrue
}
// otherwise, do some more junk here
returnfalse; // and return false
}
So the function is not returning true if the word is even, it's returning true if the word is empty.
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!