Skip to content

Instantly share code, notes, and snippets.

@goog
Created December 29, 2021 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goog/f41b7b7fcdb7e9de93606c370c9d22b5 to your computer and use it in GitHub Desktop.
Save goog/f41b7b7fcdb7e9de93606c370c9d22b5 to your computer and use it in GitHub Desktop.
char *strtok(char *str, const char *delimiters)
{
char *token = NULL;
static char *begin = NULL;
if(str != NULL)
{
begin = str;
}
else
{
if(begin == NULL)
return NULL;
}
char *p = begin; // scan begin location
//printf("p %p -%c-\n", p, *p);
while(*p)
{
if(strchr(delimiters, *p) != NULL)
{
*p = '\0';
p++;
break;
}
p++;
}
token = begin;
if(*p == '\0') // reach original string ending
{
begin = NULL;
}
else
{
begin = p; // new begin
}
return token;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment