fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iterator>
  7.  
  8. using namespace std;
  9.  
  10. std::vector<std::string> sentVec;
  11.  
  12. void getSent(string sent)
  13. {
  14. // remove the periods(s)
  15. auto iter = std::remove_if(sent.begin(), sent.end(), [] (char ch) { return ch == '.';});
  16. sent.erase(iter, sent.end());
  17.  
  18. // copy the daa to a vector
  19. std::istringstream iss(sent);
  20. string currentword;
  21. while ( iss >> currentword)
  22. sentVec.push_back(currentword);
  23. }
  24.  
  25. int main()
  26. {
  27. getSent("I am what am I.");
  28. std::vector<std::string> backward = sentVec;
  29. std::reverse(backward.begin(), backward.end());
  30. cout << "Forward:" << endl;
  31. copy(sentVec.begin(), sentVec.end(), ostream_iterator<string>(cout, " "));
  32. cout << "\nBackwards:" << endl;
  33. copy(backward.begin(), backward.end(), ostream_iterator<string>(cout, " "));
  34. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Forward:
I am what am I 
Backwards:
I am what am I