Advertisement
Spocoman

01. Format Lines

Feb 1st, 2024
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     string line, word;
  10.     getline(cin, line);
  11.  
  12.     list<string> words;
  13.  
  14.     while (line != "###") {
  15.         istringstream ss(line);
  16.         while (ss >> word) {
  17.             words.push_back(word);
  18.         }
  19.         getline(cin, line);
  20.     }
  21.  
  22.     int rowLength;
  23.     cin >> rowLength;
  24.  
  25.     string outputLine = "";
  26.  
  27.     for (auto& w : words) {
  28.         if (outputLine.length() + w.length() <= rowLength) {
  29.             outputLine += w + ' ';
  30.         }
  31.         else {
  32.             cout << outputLine.substr(0, outputLine.size() - 1) << endl;
  33.             outputLine = w + ' ';
  34.         }
  35.     }
  36.  
  37.     cout << outputLine << endl;
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement