fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <cctype>
  5.  
  6. const std::set<char> consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q',
  7. 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
  8. bool isConsonant(char c)
  9. {
  10. return consonants.count( std::tolower(c) );
  11. }
  12.  
  13. int main()
  14. {
  15. std::cout << "Please enter a word.\n";
  16. std::string word;
  17. getline(std::cin, word);
  18.  
  19. std::set<char> unique_consonants;
  20. for(size_t pos = 0; pos < word.length(); ++pos)
  21. if(isConsonant(word[pos]))
  22. unique_consonants.insert( std::tolower(word[pos]) );
  23. std::cout << "Number of different consonant= " << unique_consonants.size() << '\n';
  24. }
Success #stdin #stdout 0s 3068KB
stdin
Hello
stdout
Please enter a word.
Number of different consonant= 2