Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- В задачах предполагается, что текст разбит на слова. Под словом понимается группа символов, не содержащая пробелов и отделенная пробелами (одним или несколькими) от других слов.
- Синтаксическую правильность написания слов проверять не надо.
- Предложение (если есть такая задача) может заканчиваться: точкой (.), !, ?
- Введите строку. Добавьте перед последним символом строки три вопросительных знака.
- Написать программу, которая вводит текст, состоящий из нескольких предложений, и определяет, сколько в нем слов, состоящих из трех букв
- *******************************************************************************/
- #include <ctime>
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- const int MAX_WORD_SIZE = 10;
- string GetText(int words_count, int sentences_count)
- {
- string text;
- const int min = (int)('a');
- const int max = (int)('z');
- const vector<char> end_of_sentences_symbols = { '.', '!', '?' };
- for (int a = 0; a < sentences_count; a++)
- {
- bool is_first_word_in_sentence = true;
- for (int b = 0; b < words_count; b++)
- {
- int word_size = 1 + rand() % MAX_WORD_SIZE;
- string word;
- for (int i = 0; i < word_size; i++)
- {
- char letter = min + rand() % (max - min + 1);
- if (is_first_word_in_sentence)
- {
- is_first_word_in_sentence = false;
- letter -= ('a' - 'A');
- }
- word += letter;
- }
- if (!text.empty())
- {
- word = " " + word;
- }
- text += word;
- }
- int idx = rand() % end_of_sentences_symbols.size();
- text += end_of_sentences_symbols[idx];
- }
- return text;
- }
- int main()
- {
- srand(time(0));
- int words_count;
- int sentences_count;
- cin >> words_count >> sentences_count;
- string str = GetText(words_count, sentences_count);
- int count_of_three_letter_words = 0;
- int prev = 0;
- int pos = 0;
- while ((pos = str.find_first_of(" ", prev)) != string::npos)
- {
- if (pos - prev == 3)
- {
- count_of_three_letter_words++;
- }
- else if (pos - prev == 4 && (str[pos - 1] == '.' || str[pos - 1] == '!' || str[pos - 1] == '?'))
- {
- count_of_three_letter_words++;
- }
- prev = pos + 1;
- }
- //Так как мы сами генерируем текст, и делаем это так, что предложение всегда завершается символом ./!/?
- //- можно не делать проверку наличия символа завершения предложения
- if (prev < str.length() && str.size() - prev == 4)
- {
- count_of_three_letter_words++;
- }
- cout << str << '\n' << count_of_three_letter_words << endl;
- }
Add Comment
Please, Sign In to add comment