urgent, need help on iterator

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?
What's with the list<char>. Do you store each word in a separate list? You should change to string. It would greatly simplify the process.
Here's the code for string (one word per string):
1
2
3
4
5
6
void modify(std::string& word){
   int ea = word.find("ea");
   if(ea != std::string::npos ) word.erase(ea+1, 1);

   if(word.length() > 3 && word[word.length()-1] == 'e') word.erase(word.length()-1);
}

(It's not tested)
or store the words in a character array and run through a foreach loop and comparing each character.
oh, but my instructor only allows me to use list<char> in this task, and all characters are stored in the same list~!
Really? Can someone knock me out before I do something radical? @.@

That aside, you need to do the removal-based-on-length after you do the ea contraction. Right now, you're doing it before (assuming I'm not too sleepy).

Best of luck!

-Albatross
Last edited on
Topic archived. No new replies allowed.