Posted By

iTony on 10/26/08


Tagged

trim c strings


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


Trim function for strings in C++


 / Published in: C++
 

URL: http://blog.srikanths.net/2008/10/trim-for-c.html

  1. void trim(char *s, const int len)
  2. {
  3. int end = len - 1;
  4. int start = 0;
  5. int i = 0;
  6.  
  7. while ((start < len) && (s[start] <= ' '))
  8. {
  9. start++;
  10. }
  11.  
  12. while ((start < end) && (s[end] <= ' '))
  13. {
  14. end--;
  15. }
  16.  
  17. if (start > end)
  18. {
  19. memset(s, '\0', len);
  20. return;
  21. }
  22.  
  23. for (i = 0; (i + start) <= end; i++)
  24. {
  25. s[i] = s[start + i];
  26. }
  27. memset((s + i), '\0', len - i);
  28. }

Report this snippet  

You need to login to post a comment.