fork(1) download
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. char *trimRight(char *s)
  7. {
  8. char *spc=0, *p=s;
  9.  
  10. while (*p)
  11. if (*p == ' ')
  12. for (spc=p; *++p==' '; );
  13. else
  14. ++p;
  15.  
  16. if (spc && p!=s && p[-1]==' ') *spc = 0;
  17.  
  18. return s;
  19. }
  20.  
  21. int main()
  22. {
  23. char s[256];
  24.  
  25. cout << '"' << trimRight(strcpy(s, "")) << '"' << endl;
  26. cout << '"' << trimRight(strcpy(s, "abc qwe zzz ")) << '"' << endl;
  27. cout << '"' << trimRight(strcpy(s, "abc qwe zzz ")) << '"' << endl;
  28. cout << '"' << trimRight(strcpy(s, "abc qwe zzz u")) << '"' << endl;
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 4512KB
stdin
Standard input is empty
stdout
""
"abc qwe zzz"
"abc  qwe  zzz"
"abc  qwe  zzz   u"