fork download
  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. char *trim(char *text) {
  5. char *start = text; // points to the first character
  6. char *end = text + strlen(text); // points past the last character
  7.  
  8. // Let's start with the left side.
  9. // Move the pointer as long as there's a whitespace
  10. for (; isspace(*start); ++start);
  11.  
  12. // In a similar way, let's get the right end.
  13. // Again, move the pointer as long as there's a whitespace
  14. for (;end > start && isspace(end[-1]); --end);
  15.  
  16. // Insert the new terminator
  17. *end = '\0';
  18.  
  19. // Return the new starting offset
  20. return start;
  21. }
  22.  
  23. int main(int argc, char **argv) {
  24. char string[] = " \t Hello World\t\t\n";
  25. printf("result: '%s'\n", trim(string));
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
result: 'Hello World'