Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. char **strToWordArray(char *str, const char *delimiter)
  2. {
  3. char **words;
  4. int nwords = 1;
  5. words = malloc(sizeof(*words) * (nwords + 1));
  6.  
  7. int w = 0;
  8. int len = strlen(delimiter);
  9. words[w++] = str;
  10. while (*str)
  11. {
  12. if (strncmp(str, delimiter, len) == 0)
  13. {
  14. for (int i = 0; i < len; i++)
  15. {
  16. *(str++) = 0;
  17. }
  18. if (*str != 0) {
  19. nwords++;
  20. char **tmp = realloc(words, sizeof(*words) * (nwords + 1));
  21. words = tmp;
  22. words[w++] = str;
  23. } else {
  24. str--;
  25. }
  26. }
  27. str++;
  28. }
  29. words[w] = NULL;
  30. return words;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement