Trim in ANSII C


/ Published in: C
Save to your folder(s)

These functions will allow you to trim left and right on a string


Copy this code and paste it in your HTML
  1. char *trim(char *str)
  2. {
  3. char *out = str;
  4. out = trim_left(out);
  5. out = trim_right(out);
  6. return out;
  7. }
  8.  
  9. char *trim_left(char *str)
  10. {
  11. if(str[0] != ' ' || str[0] == '\0') return str;
  12. return trim_left(str++);
  13. }
  14.  
  15. char *trim_right(char *str)
  16. {
  17. int len = strlen(str) - 1;
  18. if(len == 0 || str[len] != ' ') return str;
  19. str[strlen(str)-1] = '\0';
  20. return trim_right(str);
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.