strtok


Token division

[Classification]

Standard library

[Syntax]

#include <string.h>

char __far * __far strtok(char __far *s1, const char __far *s2);

[Return value]

Returns a pointer to a token. If a token does not exist, the null pointer is returned.

[Description]

This function divides the character string indicated by s1 into strings of tokens by delimiting the character string with a character in the character string indicated by s2. If this function is called first, s1 is used as the first argument. Then, calling with the null pointer as the first argument continues. The delimiting character string indicated by s2 can differ on each call.

On the first call, the character string indicated by s is searched for the first character not included in the delimiting character string indicated by s2. If such a character is not found, a token does not exist in the character string indicated by s1, and strtok returns the null pointer. If a character is found, that character is the beginning of the first token.

After that, strtok searches from the position of that character for a character included in the delimiting character string at that time. If such a character is not found, the token is expanded to the end of the character string indicated by s1, and the subsequent search returns the null pointer. If a character is found, the subsequent character is overwritten by the null character (\0) indicating the termination of the token. strtok saves the pointer indicating the subsequent character. The next search for a token starts from there.

In the second or subsequent call with the null pointer as the first argument, the search starts from where the retained pointer indicates. If the null pointer is used as the value of the first argument, a code that is not re-entrant is returned.