fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <cstddef>
  4. // consider 128 ASCII decimal and their coresponding character codes
  5. int charASCIIArray[128] = {0};
  6.  
  7. void count_char(const std::string& s)
  8. {
  9. for(const auto& it: s)
  10. {
  11. if(('A' <= it && it <= 'Z') || // if char between (A,B,....,Z) or
  12. ('a' <= it && it <= 'z') ) // between (a,b,....,z)
  13. charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
  14. }
  15. }
  16.  
  17. int main()
  18. {
  19. std::string userinput = "random words WITH *- aLl";
  20.  
  21. count_char(userinput);
  22. for(std::size_t index = 0; index < 128; ++index)
  23. if(charASCIIArray[index] != 0)
  24. std::cout << "Letter " << static_cast<char>(index) // convert back to char
  25. << " occured " << charASCIIArray[index] << " times.\n";
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 4296KB
stdin
Standard input is empty
stdout
Letter H occured 1 times.
Letter I occured 1 times.
Letter L occured 1 times.
Letter T occured 1 times.
Letter W occured 1 times.
Letter a occured 2 times.
Letter d occured 2 times.
Letter l occured 1 times.
Letter m occured 1 times.
Letter n occured 1 times.
Letter o occured 2 times.
Letter r occured 2 times.
Letter s occured 1 times.
Letter w occured 1 times.