fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char * mystrtok(char *, char *);
  5.  
  6. int main ()
  7. {
  8. char str[] = "one";//Yellow fox jumps over the lazy dog";
  9. char *delim = " ";
  10. char *token;
  11.  
  12. /* get the first token */
  13. token = mystrtok(str, delim);
  14.  
  15. /* iterate through other tokens */
  16. while(token)
  17. {
  18. printf( "\n%s", token );
  19. token = mystrtok(NULL, delim);
  20. }
  21.  
  22. return 0;
  23. }
  24.  
  25. char * mystrtok(char * src, char * delim)
  26. {
  27. static char * mystr = 0;
  28. char * pos;
  29.  
  30. if(src)
  31. { /*The function was called for the first time: set the static variable and return first token*/
  32. mystr = src;
  33. if(pos = strstr(mystr, delim))
  34. {
  35. *pos = '\0';
  36. mystr = ++pos;
  37. return src;
  38. }
  39. /*This part of the code is executed when the last token is reached.
  40.   Return it and set static pointer to zero to finish it off*/
  41. src = mystr;
  42. mystr = 0;
  43. return src;
  44. }
  45. else
  46. { /*Function is called second and consecutive times. Reuse the logic by calling itself with substituted first parameter*/
  47. return (mystr ? mystrtok(mystr, delim) : 0);
  48. }
  49. }
Success #stdin #stdout 0.01s 5284KB
stdin
45
stdout
one