fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. int main()
  7. {
  8. std::string inputString;
  9. std::cout << "Enter the string = ";
  10. std::getline(std::cin, inputString);
  11.  
  12. std::map<std::string, int> Map; // word, no. of times
  13. size_t wordCount = 0;
  14. size_t letterCount = 0;
  15.  
  16. std::stringstream sstr(inputString);
  17. std::string word;
  18.  
  19. while (std::getline(sstr, word, ' '))
  20. {
  21. Map[word]++;
  22. wordCount++;
  23. letterCount += word.size();
  24. }
  25.  
  26. std::cout << "Total Words: " << wordCount << "\n\n";
  27. std::cout << "Total letters: " << letterCount << "\n\n";
  28. std::cout << "Each words count\n\n" ;
  29.  
  30. for(const auto& it: Map)
  31. std::cout << it.first << " " << it.second << " times\n";
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4356KB
stdin
This is the test sentence of the test 
stdout
Enter the string = Total Words: 8

Total letters: 30

Each words count

This 1 times
is 1 times
of 1 times
sentence 1 times
test 2 times
the 2 times