Advertisement
Guest User

02_Task2_3_Format_Lines

a guest
Jul 15th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. // 01_02_Task2_3_Format_Lines.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream> //for std::cout
  6. #include <string>
  7. #include <sstream>
  8. #include <iomanip>    
  9. #include <cctype>      //char toupper, tolower        
  10. #include <vector>          
  11. #include <iterator> //for std::ostream_iterator
  12. #include <algorithm> //for std::copy    
  13. #include <utility>  //pair<vey, value>
  14.  
  15. using namespace std;
  16.  
  17. void PrintV(vector<string> v)
  18. {
  19.     std::copy(v.begin(), v.end(), ostream_iterator<string>(cout, ", "));
  20. }
  21.  
  22. int main()
  23. {
  24.     cin.sync_with_stdio(false);
  25.     cout.sync_with_stdio(false);
  26.  
  27.     string line;
  28.     getline(cin, line);
  29.     ostringstream oss;
  30.     while (line != "###")
  31.     {
  32.         oss << " " << line << " ";
  33.         getline(cin, line);
  34.     }
  35.     string allLines = oss.str();
  36.     cout << allLines << endl;
  37.  
  38.     int width;
  39.     cin >> width;
  40.  
  41.     ostringstream oss2; string word;
  42.     int oss2Width = 0, wordWidth = 0;
  43.  
  44.     istringstream iss(allLines);
  45.    
  46.     while (iss >> word)
  47.     {
  48.         oss2Width = oss2.str().size();
  49.         if (oss2Width + word.size() > width)
  50.         {
  51.             cout << oss2.str() << endl;
  52.             oss2.str("");
  53.             oss2.clear();
  54.             oss2 << word  << " ";
  55.         }
  56.         else
  57.         {
  58.             oss2 << word << " ";           
  59.         }      
  60.     }
  61.     cout << oss2.str();
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement