fork(1) download
  1. #include <stdio.h>
  2.  
  3. void string_trim(char *input) {
  4. char *dest=input;
  5. while(*input==' ') {
  6. input++;
  7. }
  8. char *end=input;
  9. while(*input) {
  10. if((*dest++=*input++)!=' ') {
  11. end=dest;
  12. }
  13. }
  14. *end=0;
  15. }
  16.  
  17. void test(const char *input) {
  18. char *work=malloc(strlen(input)+1);
  19. strcpy(work,input);
  20. string_trim(work);
  21. printf("*%s* -> *%s*\n",input,work);
  22. free(work);
  23. }
  24.  
  25. int main(void) {
  26. test("test");
  27. test(" test");
  28. test(" test");
  29. test("test ");
  30. test("test ");
  31. test(" te st ");
  32. test("t");
  33. test(" t");
  34. test("t ");
  35. test(" ");
  36. test("");
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 5476KB
stdin
Standard input is empty
stdout
*test* -> *test*
* test* -> *test*
*  test* -> *test*
*test * -> *test*
*test  * -> *test*
*  te st  * -> *te st*
*t* -> *t*
* t* -> *t*
*t * -> *t*
*  * -> *  *
** -> **