fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. string input;
  11. cout << "Введите строку: ";
  12. getline(cin, input); // Чтение всей строки с пробелами
  13.  
  14. vector<string> words;
  15. string word;
  16. istringstream iss(input);
  17.  
  18. // Разбиваем строку на слова
  19. while (iss >> word) {
  20. words.push_back(word);
  21. }
  22.  
  23. if (words.empty()) {
  24. cout << "Строка пуста!" << endl;
  25. return 0;
  26. }
  27.  
  28. // Находим слово максимальной длины
  29. auto max_it = max_element(words.begin(), words.end(),
  30. [](const string& a, const string& b) {
  31. return a.length() < b.length();
  32. });
  33.  
  34. string longest_word = *max_it;
  35. int word_index = distance(words.begin(), max_it) + 1; // Порядковый номер (начиная с 1)
  36. int char_position = input.find(longest_word); // Позиция в строке (начиная с 0)
  37.  
  38. cout << "Слово максимальной длины: \"" << longest_word << "\"" << endl;
  39. cout << "Порядковый номер: " << word_index << endl;
  40. cout << "Начинается с позиции: " << char_position << endl;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Введите строку: Строка пуста!