fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::string line;
  9.  
  10. // get input from cin stream
  11. if (std::getline(std::cin, line)) // check for success
  12. {
  13. std::vector<std::string> words;
  14. std::string word;
  15.  
  16. // The simplest way to split our line with a ' ' delimiter is using istreamstring + getline
  17. std::istringstream stream;
  18. stream.str(line);
  19.  
  20. // Split line into words and insert them into our vector "words"
  21. while (std::getline(stream, word, ' '))
  22. words.push_back(word);
  23.  
  24. if (words.size() % 2 != 0) // if word is not even, print error.
  25. std::cout << "Word count not even " << words.size() << " for string: " << line;
  26. else
  27. {
  28. //Remove the last word from the vector to make it odd
  29. words.pop_back();
  30.  
  31. std::cout << "Original: " << line << std::endl;
  32. std::cout << "New:";
  33.  
  34. for (std::string& w : words)
  35. std::cout << " " << w;
  36. }
  37. }
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3468KB
stdin
All The Presidents Men
stdout
Original: All The Presidents Men
New: All The Presidents