Код на C++ Испоьзовать только стандартные библиотеки string и iostream Условие: Подсчитать количество слов, разделенных запятыми, содержащих k гласных букв (k-задается с экрана).
нелепость, нелепость, вот пример кода на C++, который решает вашу задачу: #include <iostream> #include <string> using namespace std; int countVowels(string word) { int count = 0; for (int i = 0; i < word.length(); i++) { char c = tolower(word[i]); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { count++; } } return count; } int main() { int k; cout << "Введите количество гласных букв: "; cin >> k; string input; cout << "Введите строку: "; getline(cin, input); int count = 0; for (int i = 0; i < input.length(); i++) { if (input[i] == ',') { string word = input.substr(0, i); input = input.substr(i + 1); if (countVowels(word) == k) { count++; } i = -1; } } if (countVowels(input) == k) { count++; } cout << "Количество слов, содержащих " << k << " гласных букв: " << count << endl; return 0; } Code #include <iostream> #include <string> using namespace std; int countVowels(string word) { int count = 0; for (int i = 0; i < word.length(); i++) { char c = tolower(word[i]); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { count++; } } return count; } int main() { int k; cout << "Введите количество гласных букв: "; cin >> k; string input; cout << "Введите строку: "; getline(cin, input); int count = 0; for (int i = 0; i < input.length(); i++) { if (input[i] == ',') { string word = input.substr(0, i); input = input.substr(i + 1); if (countVowels(word) == k) { count++; } i = -1; } } if (countVowels(input) == k) { count++; } cout << "Количество слов, содержащих " << k << " гласных букв: " << count << endl; return 0; } Код считывает количество гласных букв `k` и строку `input` с помощью `getline()`. Затем он разбивает строку на слова, разделенные запятыми, и проверяет каждое слово на количество гласных букв с помощью функции `countVowels()`. Если количество гласных букв равно `k`, то счетчик `count` увеличивается. В конце программа выводит количество слов, содержащих `k` гласных букв.
#include <iostream> #include <string> using namespace std; bool isalpha(char s) { return !(string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").find(s) == string::npos); } bool isvowel(char s) { return !(string("aeiouyAEIOUY").find(s) == string::npos); } int wordcount(const string& text, int k){ int count = 0; for (auto i = text.begin(); i != text.end(); ++i) { int vowelcount = 0; while (isalpha(*i)) { if (isvowel(*i)) ++vowelcount; ++i; if (i == text.end()) return count; } if (vowelcount == k) ++count; } return count; } int main() { int k; string text; cin >> k; cin.ignore(); getline(cin, text); cout << '\n' << wordcount(text, k); } C #include <iostream> #include <string> using namespace std; bool isalpha(char s) { return !(string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").find(s) == string::npos); } bool isvowel(char s) { return !(string("aeiouyAEIOUY").find(s) == string::npos); } int wordcount(const string& text, int k){ int count = 0; for (auto i = text.begin(); i != text.end(); ++i) { int vowelcount = 0; while (isalpha(*i)) { if (isvowel(*i)) ++vowelcount; ++i; if (i == text.end()) return count; } if (vowelcount == k) ++count; } return count; } int main() { int k; string text; cin >> k; cin.ignore(); getline(cin, text); cout << '\n' << wordcount(text, k); }
код не учитывал последнюю гласную, если она последний символ в строке строка 20 изменена if (i == text.end()) return (vowelcount == k)? count+1 : count; C if (i == text.end()) return (vowelcount == k)? count+1 : count;