Posted By

pjaicher on 10/26/12


Tagged

trim String right char Left


Versions (?)

Trim in ANSII C


 / Published in: C
 

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

  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  

You need to login to post a comment.