fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <vector>
  6. #include <locale>
  7. #include <cassert>
  8.  
  9. template <typename TContainer,typename TString>
  10. void tokenize(TString input,TContainer& res)
  11. {
  12.  
  13. if (input.length()<2) {
  14. res.push_back(input);
  15. return;
  16. }
  17.  
  18. typename TString::const_iterator pos=input.begin();
  19. bool space_state=std::isspace(input[0],std::locale());
  20. for (typename TString::const_iterator it=input.begin(); it!=input.end();
  21. ++it) {
  22. bool is_space=std::isspace(*it,std::locale());
  23. if (is_space!=space_state) {
  24. res.push_back(TString(pos,it));
  25. pos=it;
  26. space_state=is_space;
  27. }
  28. }
  29.  
  30. //the rest
  31. if (pos!=input.end()) {
  32. res.push_back(TString(pos,(typename TString::const_iterator)input.end()));
  33. }
  34.  
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40. std::string test("hi how are you"),reference("ih woh era uoy");
  41.  
  42. std::vector<std::string> tokens;
  43. tokenize(test,tokens);
  44.  
  45. for (auto& token : tokens)
  46. std::reverse(token.begin(),token.end());
  47.  
  48. std::stringstream buf;
  49. for (auto token : tokens)
  50. buf<<token;
  51.  
  52. std::string res=buf.str();
  53. assert(res==reference);
  54.  
  55. std::cout<<res<<std::endl;
  56. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
ih   woh era uoy