How does strtok() split the string into tokens in C?

How does strtok() split the string into tokens in C?

the strtok runtime function works like this

the first time you call strtok you provide a string that you want to tokenize

char s[] = this is a string;

in the above string space seems to be a good delimiter between words so lets use that:

char* p = strtok(s,  );

what happens now is that s is searched until the space character is found, the first token is returned (this) and p points to that token (string)

in order to get next token and to continue with the same string NULL is passed as first
argument since strtok maintains a static pointer to your previous passed string:

p = strtok(NULL, );

p now points to is

and so on until no more spaces can be found, then the last string is returned as the last token string.

more conveniently you could write it like this instead to print out all tokens:

for (char *p = strtok(s, ); p != NULL; p = strtok(NULL,  ))


If you want to store the returned values from strtok you need to copy the token to another buffer e.g. strdup(p); since the original string (pointed to by the static pointer inside strtok) is modified between iterations in order to return the token.

strtok() divides the string into tokens. i.e. starting from any one of the delimiter to next one would be your one token. In your case, the starting token will be from – and end with next space . Then next token will start from and end with ,. Here you get This as output. Similarly the rest of the string gets split into tokens from space to space and finally ending the last token on .

How does strtok() split the string into tokens in C?

strtok maintains a static, internal reference pointing to the next available token in the string; if you pass it a NULL pointer, it will work from that internal reference.

This is the reason strtok isnt re-entrant; as soon as you pass it a new pointer, that old internal reference gets clobbered.

Leave a Reply

Your email address will not be published. Required fields are marked *