Trim function for strings in C++


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



Copy this code and paste it in your 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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.