fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <locale>
  5. #include <cassert>
  6.  
  7.  
  8. int main()
  9. {
  10. std::string test("hi how are you"),reference("ih woh era uoy");
  11.  
  12. std::locale const& loc=std::locale();
  13.  
  14. auto pos=test.begin();
  15.  
  16. auto next_token=
  17. [&](char c) {
  18. return std::isspace(c,loc)!=std::isspace(*pos,loc);
  19. };
  20.  
  21. for (auto it=std::find_if(pos,test.end(),next_token);
  22. it!=test.end();
  23. it=std::find_if(pos,test.end(),next_token)) {
  24. if (!std::isspace(*pos,loc))
  25. std::reverse(pos,it);
  26. pos=it;
  27. }
  28. std::reverse(pos,test.end());
  29.  
  30. std::cout<<test<<std::endl;
  31.  
  32. assert(test==reference);
  33. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
ih   woh era uoy