Posted By

zava on 08/12/10


Tagged

textmate String sub


Versions (?)

substr.c


 / Published in: C
 

  1. // Extract a substring from an existing string, return the length of the substring
  2. // dst: receive the substring
  3. // src: the source string to extract from
  4. // start: extract start with the 'src' + 'start' char, zero based; no bound check
  5. // len: length of the substring; may be shorter than expected.
  6. // Ansi string only!
  7.  
  8. int substr(char* dst, char* src, int start, int len)
  9. {
  10. char* d = dst;
  11. char* s = src + start;
  12. while (len-- > 0)
  13. {
  14. if (*s == '\0')
  15. {
  16. *d = '';
  17. return d - dst;
  18. }
  19. *d++ = *s++;
  20. }
  21. *d = '\0';
  22. return d - dst;
  23. }
  24.  
  25. int main()
  26. {
  27. char s[1024] = { '\0' };
  28. printf("%dt%sn", substr(s, "0123456789", 3, 10), s);
  29. }

Report this snippet  

You need to login to post a comment.