fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. void count_occurances(const string& the_line)
  9. {
  10. map<char, unsigned> counts;
  11.  
  12. for(char c: the_line)
  13. if(isalpha(c))
  14. ++counts[toupper(c)];
  15.  
  16. for(const auto& p: counts)
  17. cout << char(tolower(p.first)) << " or " << p.first << " occurred: "
  18. << p.second << " times\n";
  19. }
  20.  
  21. int main()
  22. {
  23. cout << "Enter line\n"
  24. ">>";
  25. string line;
  26. getline(cin, line);
  27.  
  28. count_occurances(line);
  29. }
  30.  
Success #stdin #stdout 0s 2992KB
stdin
This is an example
stdout
Enter line
>>a or A occurred: 2 times
e or E occurred: 2 times
h or H occurred: 1 times
i or I occurred: 2 times
l or L occurred: 1 times
m or M occurred: 1 times
n or N occurred: 1 times
p or P occurred: 1 times
s or S occurred: 2 times
t or T occurred: 1 times
x or X occurred: 1 times