fork download
  1. #include <string>
  2. #include <cctype>
  3. #include <iostream>
  4. #include <unordered_map>
  5.  
  6. std::unordered_map<char, std::string> substitute_for =
  7. {
  8. { 'A', "100 0001" }, { 'B', "100 0010" }, { 'C', "100 0011" }, { 'D', "100 0100" },
  9. { 'E', "100 0101" }, { 'F', "100 0110" }, { 'G', "100 0111" }, { 'H', "100 1000" },
  10. { 'I', "100 1001" }, { 'J', "100 1010" }, { 'K', "100 1011" }, { 'L', "100 1100" },
  11. { 'M', "100 1101" }, { 'N', "100 1110" }, { 'O', "100 1111" }, { 'P', "101 0000" },
  12. { 'Q', "101 0001" }, { 'R', "101 0010" }, { 'S', "101 0011" }, { 'T', "101 0100" },
  13. { 'U', "101 0101" }, { 'V', "101 0110" }, { 'W', "101 0111" }, { 'X', "101 1000" },
  14. { 'Y', "101 1001" }, { 'Z', "101 1010" }
  15. };
  16.  
  17. std::string convert(const std::string& text)
  18. {
  19. std::string result;
  20.  
  21. for (auto ch : text)
  22. {
  23. if (std::isalpha(ch)) result += substitute_for[std::toupper(ch)];
  24. else result += ch;
  25. }
  26.  
  27. return result;
  28. }
  29.  
  30. int main()
  31. {
  32. std::string text;
  33.  
  34. std::cout << "Enter statement you would like to convert to binary:\n> ";
  35. std::getline(std::cin, text);
  36.  
  37. std::cout << convert(text) << '\n';
  38. }
Success #stdin #stdout 0s 3480KB
stdin
hi how is it going
stdout
Enter statement you would like to convert to binary:
> 100 1000100 1001 100 1000100 1111101 0111 100 1001101 0011 100 1001101 0100 100 0111100 1111100 1001100 1110100 0111