Advertisement
Guest User

Untitled

a guest
Dec 29th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. int main() {
  6.   std::string line, text;
  7.  
  8.   while (getline(std::cin, line).good() && line.find("###") == std::string::npos) {
  9.     text += line + " ";
  10.   }
  11.  
  12.   std::istringstream iss(text);
  13.  
  14.   size_t maxLength;
  15.   std::cin >> maxLength;
  16.  
  17.   std::ostringstream out;
  18.   std::string word;
  19.   size_t currentLength = 0;
  20.  
  21.   while (iss >> word) {
  22.     if (currentLength > 0) {
  23.       if (word.length() + currentLength < maxLength) {
  24.         out << " ";
  25.         ++currentLength;
  26.       } else {
  27.         out << std::endl;
  28.         currentLength = 0;
  29.       }
  30.     }
  31.  
  32.     out << word;
  33.     currentLength += word.length();
  34.   }
  35.  
  36.   std::cout << out.str() << std::endl;
  37.  
  38.   return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement