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'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!
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).
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?
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
constint 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];
}
}
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.