hi guys,
i got a task which needs to change words with a series of rules
1. if that word contain 'ea', get rid of 'a'
2. if that word longer than 3 characters and end with 'e', get rid of that 'e'
eg. "here" -> "her"
"ease" -> "ese" why? because after change "ease" to "ese" it only contains 3 characters.
p.s. the words are store in a list<char>, which means it's stored character by character.
so, here is 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
|
void modify(list<char> words)
{
list<char>::iterator iterator3;
list<char>::iterator iterator2 = words.end();
list<char>::iterator iterator1 = words.end();
int wordLength == 0;
for(iterator3 = words.begin(); iteraotr3 != words.end(); iterator3++) {
if(wordLength >= 3 && !isalpha(*iterator3) && *iterator2 == 'e')
words.erase(iterator2);
else if(*iterator1 == 'e' && *iterator2 == 'a')
words.erase(iterator2);
countLength(iterator1, wordLength);
iterator1 = iterator2;
iterator2 = iterator3;
}
}
void countLength(list<char>::iterator it, int &wordLength)
{
if(!isalpha(*it))
wordLength = 0;
else
wordLength++;
}
|
in this code, i create 3 iterators to take three places and check them at a time
if list contains "here ease"
the expected output should be "her ese"
but how comes i got "her es"
how can i change to get the expect result?