Tell me, please, how can I find a line with the maximum number of words and output this result (the text is read from the file)

#include "stdafx.h" #include <iostream> #include <fstream> #include <conio.h> #include <string> #include <cstdlib> #include <algorithm> #pragma warning(disable : 4996) using namespace std; int main() { setlocale(LC_ALL, "Russian"); int otv; do { cout << "1.Кодировка" << endl << "2.Декодировка" << endl << "0.Выход" << endl; int count; cin >> count; if (count != 1 && count != 2 && count != 0) { cout << endl << "Будьте внимательней!" << endl << endl; } switch (count) { case 1: { string s; int strok = 0; int max = 0; ifstream book("text.txt"); if (book.is_open()) { cout << endl << "Текст загружен!" << endl << endl; } else { cout << "Текст не загружен!" << endl << endl; } while (getline(book, s)) { strok++; } cout << "Количество строк :" << strok << endl << "Максимальное количество слов в строке :" << max << endl << endl; break; } case 2: { break; } case 0: { exit(0); } } } while (otv = 1); _getch(); return 0; } 
  • First of all, remove all the nonsense that you wrote ... - AR Hovsepyan

2 answers 2

Well, if you use STL then the answer is: (Everything is noted)

 // Example program #include <iostream> #include <string> #include <algorithm> #include <vector> //Разделить строку по пробелам std::vector<std::string> splitStr(std::string s) { std::vector<std::string> strs; //Вектор строк size_t pos = 0; //Итератор while ((pos = s.find(" ")) != std::string::npos) { //Повторять пока не найдено пробелов strs.push_back(s.substr(0, pos)); //Добавить строку в вектор s.erase(0, pos + 1); //Удалить значение из строки } return strs; // Вернуть вектор строк } int main() { std::vector<std::string> a {"This is the longest", "aaaaaa", "Tiny", "Medium"}; //Вектор для примера std::vector<std::string>::iterator it = std::max_element( //Найти максимальный элемент a.begin(), // Начало вектора `a` a.end(), // Конец вектора `a` [](std::string &A, std::string &B) // Лямбда функция с 2 параметрами { return splitStr(A).size()<splitStr(B).size(); //Вернуть если размер разделенной строки A меньше размера разделенной строки B } ); std::cout << *it << std::endl; // Вывести значение } 

If we talk about the "vanilla" methods, then here (also commented out)

 // Example program #include <iostream> #include <string> int main() { int size = 4; // Размер массива std::string *a = new std::string[size] {"This is the longest", "aaaaaa", "Tiny", "Medium"}; //Массив строк `a` int max_spaces = 0; //Максимальное кол-во пробелов найдено int max_index = 0; //Индекс с максимальным кол-вом пробелов for (int i = 0; i < size; i++) { int currentSpaces = 0; //Кол-во пробелов у текущей строки for (int j = 0; j < a[i].size(); j++) { if (a[i][j] == ' ') { currentSpaces++; } } if (currentSpaces > max_spaces) //Если у текущей строки больше пробелов { max_index = i; // То максимальный индекс равен i (текущая строка) } } std::cout << a[max_index]; // Вывести } 

    #ThisCode

     #include <iostream> #define IN 1 #define OUT 0 int main() { const unsigned int size = 5; std::string str[size] = {"sdad sad ", "sad sa da", " adawfs sd sdfds r crc rc ", " ", " dsa"}; int word[size] ={0}; bool state = OUT; for (unsigned int i =0; i < size; ++i){ for(char it: str[i]){ if(it == ' ' || it == '\n' || it == '\t'){ state = OUT; } else if(state == OUT){ state = IN; ++word[i]; } } std::cout << "Слов в строке " << i << ":"<< word[i] << std::endl; } return 0; }