# Counting words occurences

• 06-04-2010
darren78
Counting words occurences
Hi,

I've just done an exercise from a book that counts the number of occurences of a word in an input. I have done this, and it works. However, i'm not sure if this is a good way to do it? I was a little confused at the start regarding the loop/vector off by one idea and tried to work around that. I am aware of strcmp, however I have not reached that yet in the book and as such, tried to do this without using that function. Any comments would be welcome.

Code:

```void CheckWords(const Vector<string>& wds) {         int counter = 1;         for(int i = 0; i<wds.size()-1; ++i){                 //check if word equal to next word, if so increment counter                 if(wds[i] == wds[i+1])                         ++counter;                 else{//if not equal, print the word and the number of occurences                         cout << wds[i] << " Counter: " << counter << endl;                         counter = 1;                 }                 //check if word is penultimate and if matches last word or not.                 if(i == wds.size()-2){                         if(wds[i] == wds[i+1]){                                 cout << wds[i] << "counter: " << counter << endl;                         counter = 1;                         }                         if(wds[i] != wds[i+1]){                                 cout << wds[i+1] << "counter 1" << endl;                                 counter = 1;                         }                 }         } }```
• 06-04-2010
MK27
Quote:

Originally Posted by darren78
I am aware of strcmp

Strcmp is for C style char strings. With C++ you can just use the <, >, and == operators, or .compare().

A logical problems with this:
Code:

```void CheckWords(const Vector<string>& wds) {         int counter = 1;         for(int i = 0; i<wds.size()-1; ++i){                 //check if word equal to next word, if so increment counter                 if(wds[i] == wds[i+1])                         ++counter;```
You are detecting repeated words, not duplicated words. Eg, if the vector were "one", "two", "three", "two" you would not detect the two twos.
• 06-04-2010
darren78
Quote:

Originally Posted by MK27
Strcmp is for C style char strings. With C++ you can just use the <, >, and == operators, or .compare().

A logical problems with this:
Code:

```void CheckWords(const Vector<string>& wds) {         int counter = 1;         for(int i = 0; i<wds.size()-1; ++i){                 //check if word equal to next word, if so increment counter                 if(wds[i] == wds[i+1])                         ++counter;```
You are detecting repeated words, not duplicated words. Eg, if the vector were "one", "two", "three", "two" you would not detect the two twos.

Thanks for the response. I have previously sorted the vector, so that all identical words are next to each other.

Apart from that, is the method correct , bearing in mind the contsraints I have put my self under to write the code?

Thanks again.
• 06-04-2010
laserlight
Just a note that you may find useful in the future: this particular problem can be solved by using a std::map to map words to their counts.