C/C++

Задача про строки C++(выполнить нужно внутри 1ой функции)

Ввести предложение длиной не более 80 символов. Определить длину его предпоследнего слова и количество слов короче предпоследнего. Вывести эти слова. Количество пробелов между словами произвольно.
#include <iostream>
#include <string>

using namespace std;

bool is_en_leter(const char c)
{
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}

int main()
{
string P;
do
{
getline(cin, P);
if(P.length() > 80) cerr << "Длина предложения должна быть не более 80 символов." << endl;
} while(P.length() > 80);

const char max_count_words = 80;
string w[max_count_words];
int count_words = 0;

for(int x = 0; x < P.length(); x++)
{
if( is_en_leter(P[x]) || P[x] == '-' ) // ещё дефис может быть частью слова
{
w[count_words] += P[x];
if( ! is_en_leter(P[x + 1]) && P[x + 1] != '-' )
{
count_words++;
}
}
}

// длина предпоследнего слова
int len_pred_last = w[count_words - 2].length();
cout << "Длина предпоследнего слова -- " << len_pred_last << '.'
<< endl;

// количество слов, меньше предпоследнего
int q = 0;
for(int x = 0; x < count_words; x++)
{
if(w[x].length() < len_pred_last)
{
q++;
// вывести эти слова
cout << w[x] << ' ';
}
}
cout << endl << endl;
cout << "Количество слов, меньших предпоследнего -- " << q << '.' << endl;

return 0;
}
ШБ
Шалтай Болтай
99 223
Лучший ответ
Шалтай Болтай Программа рассчитана на английский язык
!!!тёма!!! .;;шyрkин;;. А можно сделать все тоже самое, но без использования ООП?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int is_word_guessed(const char* secret, const char* letters_guessed) {
int i = 0;
while (secret[i]) {
int j = 0;
while (letters_guessed[j]) {
if (secret[i] == letters_guessed[j]) break;
++j;
}
if (j == strlen(letters_guessed)) return 0;
++i;#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <tuple>
using namespace std;
tuple<size_t, size_t, vector<string>> fn(const string& line) {
istringstream iss(line);
vector<string> box;
string word;
while (iss >> word) box.push_back(word);
auto length = (*(box.end() - 2)).length();
vector<string> words;
for (const auto& item : box) {
if (item.length() < length) {
words.push_back(item);
}
}
return tuple<size_t, size_t, vector<string>>{ length, words.size(), words };
}
int main() {
system("chcp 1251 > nul");
cout << "Строка: ";
char line[81];
cin.getline(line, size(line));
auto [length, count, words] = fn(line);
cout
<< "Длина предпоследнего слова: " << length << '\n'
<< "Количество слов короче предпоследнего: " << count << '\n';
if (!count) puts("");
else {
cout << "Слова короче предпоследнего:";
for (const auto& word : words) cout << ' ' << word;
puts("");
}
system("pause > nul");
}
Orazhan Baidauletov
Orazhan Baidauletov
82 836