fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main()
  5. {
  6. std::vector<std::string> word_vec;
  7. std::vector<size_t> word_cnt_vec;
  8. std::string input;
  9.  
  10. std::cin >> input;
  11. if (input != ".")
  12. {
  13. word_vec.push_back(input);
  14. word_cnt_vec.push_back(1);
  15.  
  16. while (std::cin >> input)
  17. {
  18. if (input != ".")
  19. {
  20. // Check if the input word is already present
  21. bool found = 0;
  22.  
  23. for (int i = 0; i < word_vec.size(); ++i)
  24. {
  25. // Increase string count
  26. if (input == word_vec[i])
  27. {
  28. word_cnt_vec[i]++;
  29. found = 1;
  30. break;
  31. }
  32. }
  33.  
  34. if (found == 0)
  35. {
  36. word_vec.push_back(input);
  37. word_cnt_vec.push_back(1);
  38. }
  39. }
  40.  
  41. else {
  42. break;
  43. }
  44. }
  45. }
  46.  
  47. // Print the words along with their count
  48. std::cout << "Word\tCount\n\n";
  49.  
  50. for (int i = 0; i < word_vec.size(); ++i) {
  51. std::cout << word_vec[i] << "\t" << word_cnt_vec[i] << "\n";
  52. }
  53. }
Success #stdin #stdout 0s 16064KB
stdin
Dam dam doi Dam stars. ... .. ... doi doi . Stars
stdout
Word	Count

Dam	2
dam	1
doi	3
stars.	1
...	2
..	1