Distinct Word Counter

Apr 11, 2014 at 2:07am
I have a homework assignment that requires the user to input some strings then the program will count the total words, distinct words, and count of each word.

Output:

Enter your sentences below. ### will end the statement.

i am doing homework i doing am i am

Total Words: 9
Distinct Words: 4

i: 3
am: 3
doing: 2
homework: 1

End Output.

Here's a link to the assignment.
http://www2.cs.uh.edu/~acl/cs1410/Assignment/prog9.pdf

I've been working on it all week and I can't figure it out. I've looked up everything I can think of but the answers are to basic and don't work in this situation. All I need is a push in the right direction because I think I'm pretty close. Any help at all is appreciated. Thanks!
Apr 11, 2014 at 2:20am
Have you considered using a std::map<std::string, int>?
Apr 11, 2014 at 2:25am
Due Date: Monday, April 7.
:P

Anyways, as LB mentioned a std::map would probably be the easiest. Though it almost sounds like the assignment says to create an array of strings then sort it and count the number of words (they will be next to each other).
Apr 11, 2014 at 2:29am
Lol he changed the due date to tomorrow because we had a test on monday.

I can get it to display the total number of words and how many times each word occurs, but mine does this:

input: i am lost am lost i i

i: 3
am: 2
lost: 2
am: 2
lost: 2
i: 3
i: 3


It does that because its in a for loop but i can't figure out how to do it any other way. We haven't learned about maps yet, how would one work in this situation?
Apr 11, 2014 at 2:34am
That's why you instructor asked you to sort it. Basically iterate over the array. Increment by 1 if the word is the same. If not output the word and the count then set the word to the current word and set the count to 1.

Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string words[] = sorted words
const int size = get number of words

int counter = 1;
std::string word = words[0];
for(int i = 1; i < size; ++i)
{
    if(words[i] == word) ++counter;
    else
    {
        std::cout << word << ": " << counter << std::endl;
        counter = 1;
        word = words[i];
    }
}
Last edited on Apr 11, 2014 at 2:37am
Apr 11, 2014 at 2:44am
Here's what I have so far. And I've moved stuff around in every way I can imagine. If anyone has any ideas let me know. Thanks for the help giblit and LB.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>

using namespace std;

struct Word {
    string word;
    int count;
};

class WordList {
    public: 
        int totalCount, distinctCount;
        Word words[200];       
};

int main() {
    WordList list;
    list.totalCount = 0;
    list.distinctCount = 0;
    bool done = false;
    int i = 0;
    int a;
    //string display[200];

    cout<<"Enter your sentences below. ### will terminate the input."<<endl;
    
    while(done == false) {
        cin>>list.words[i].word;
        list.totalCount++;
        
        if(list.words[i].word == "###") {
            list.totalCount--;
            done = true;
        }
        i++;
    }
    cout<<endl;
    
    for(int cnt = 0; cnt < list.totalCount; cnt++) {
        list.words[cnt].count = 0;
        for(a = 0; a < list.totalCount; a++) {
            if(list.words[cnt].word == list.words[a].word) {
                list.words[cnt].count++;
                //cin.ignore(' ');
            }
            else {
                
            } 
        }
        cout<<list.words[cnt].word<<" "<<list.words[cnt].count<<endl;
    }
    
    cout<<"Total Words: "<<list.totalCount<<endl;
    cout<<"Distinct Words: "<<list.distinctCount<<endl;
    
    system("pause");
    return 0;
}
Last edited on Apr 11, 2014 at 3:11am
Topic archived. No new replies allowed.