fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <vector>
  5.  
  6. int main(int argc, const char * argv[]) {
  7.  
  8. std::map<std::string, size_t> word_count;
  9.  
  10. std::set <std::string> exclude = {"The", "But", "And", "Or", "An", "A", "the", "but", "and", "or", "an", "a"};
  11.  
  12. std::string word;
  13.  
  14. while (std::cin >> word) {
  15.  
  16. if (word == "exit") {
  17. break;
  18. }
  19.  
  20. if (exclude.find(word) == exclude.end())
  21. ++word_count[word];
  22. }
  23.  
  24. for (const auto &w : word_count) {
  25.  
  26. std::cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << std::endl;
  27.  
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5312KB
stdin
Three Rings for the Elven-kings under the sky,
Seven for the Dwarf-lords in their halls of stone, 
Nine for Mortal Men doomed to die, 
One for the Dark Lord on his dark throne
In the Land of Mordor where the Shadows lie. 
One Ring to rule them all, One Ring to find them, 
One Ring to bring them all and in the darkness bind them
In the Land of Mordor where the Shadows lie. 
exit
stdout
Dark occurs 1 time
Dwarf-lords occurs 1 time
Elven-kings occurs 1 time
In occurs 2 times
Land occurs 2 times
Lord occurs 1 time
Men occurs 1 time
Mordor occurs 2 times
Mortal occurs 1 time
Nine occurs 1 time
One occurs 4 times
Ring occurs 3 times
Rings occurs 1 time
Seven occurs 1 time
Shadows occurs 2 times
Three occurs 1 time
all occurs 1 time
all, occurs 1 time
bind occurs 1 time
bring occurs 1 time
dark occurs 1 time
darkness occurs 1 time
die, occurs 1 time
doomed occurs 1 time
find occurs 1 time
for occurs 4 times
halls occurs 1 time
his occurs 1 time
in occurs 2 times
lie. occurs 2 times
of occurs 3 times
on occurs 1 time
rule occurs 1 time
sky, occurs 1 time
stone, occurs 1 time
their occurs 1 time
them occurs 3 times
them, occurs 1 time
throne occurs 1 time
to occurs 4 times
under occurs 1 time
where occurs 2 times