fork(2) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::string str = "I need to split input string";
  9. std::vector<std::string> output;
  10.  
  11. std::istringstream iss(str);
  12. std::string word;
  13. const int max = 10;
  14.  
  15. while((iss >> word))
  16. {
  17. // Check if the last element can still hold another word (+ space)
  18. if (output.size() > 0 && (output[output.size() - 1].size() + word.size() + 1) <= max)
  19. output[output.size() - 1] += ' ' + word;
  20. else
  21. output.push_back(word);
  22. }
  23.  
  24. for (auto&it : output)
  25. std::cout << it << std::endl;
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
I need to
split
input
string