fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4.  
  5. std::string spinWords( const std::string &s, std::string::size_type length = 5 )
  6. {
  7. std::string t( s );
  8. const char *delim = " \t";
  9.  
  10. for ( std::string::size_type i = 0; i != t.size(); )
  11. {
  12. auto pos = t.find_first_not_of( delim, i );
  13.  
  14. if ( pos != std::string::npos )
  15. {
  16. i = t.find_first_of( delim, pos );
  17.  
  18. if ( i == std::string::npos ) i = t.size();
  19.  
  20. if ( length < i - pos )
  21. {
  22. auto n = i - pos;
  23.  
  24. for ( std::string::size_type j = 0; j < n / 2; j++ )
  25. {
  26. std::swap( t[pos + j], t[i - j - 1] );
  27. }
  28. }
  29. }
  30. else
  31. {
  32. i = t.size();
  33. }
  34. }
  35.  
  36. return t;
  37. }
  38.  
  39. int main()
  40. {
  41.  
  42. std::string s( "1 12 123 1234 12345 123456 1234567 123456789 1234567890" );
  43.  
  44. std::cout << s << '\n';
  45. std::cout << spinWords( s ) << '\n';
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 4524KB
stdin
Standard input is empty
stdout
1 12 123 1234 12345 123456 1234567 123456789 1234567890
1 12 123 1234 12345 654321 7654321 987654321 0987654321