strtok

Searches for a character string bounded by characters in a specified set of delimiters

#include <strings.h>
char *strtok (
        char *s1,
        const char *s2
)

Arguments

s1 Pointer to target character string to search
s2 Pointer to character set containing token delimiters

Return Value

A pointer to the first character of a token is returned, or NULL is returned if there is no token.

Explanation

A sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call in the sequence has s1 as its first argument, and is followed by calls with a NULL pointer as their first argument. The first call in the sequence searches the string pointed to by s1 for the first character that is not contained in the current separator string pointed to by s2. If no such character is found, then there are no tokens in the string pointed to by s1 and the function returns a NULL pointer. If such a character is found, it is the start of the first token. strtok() then searches from there for a character that is contained in the current separator string. If no such character is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token will return a NULL pointer. If such a character is found, it is overwritten by a NULL character, which terminates the current token. strtok() saves a pointer to the following character, from which the next search for a token will start. Each subsequent call, with a NULL pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.