The problem is that the program incorrectly counts the number of consonants in general. Maybe the problem is that the compiler does not understand Russian?
#include <iostream> #include <string> using namespace std; int consonantsNumber(const string &str) { int result = 0; string consonants = "бвгджзйклмнпрстфхцчшщ"; for (int i = 0; i < str.size(); i++) for (int j = 0; j < consonants.size(); j++) if ( tolower(str[i]) == consonants[j] ) { result++; } return result; } int main() { string s; getline(cin, s); cout << consonantsNumber(s); return 0; }
stringtypefind_first_of(), etc. - they are able to search for a symbol from the provided string (i.e. it can feed yourconsonants). And you can use an associative array (map) to keep statistics on each character (letter) - again, in order to get rid of conditions. But I am not sure that this will be clearer than withif/switch. And faster, too, is unlikely to become. - Vladimirstrpbrk. Even a similar example is: cplusplus.com/reference/cstring/strpbrk (about vowels). - Vladimir