welibc
A clear, secure, and well documented standard C library
Functions
strtok.c File Reference

Defines the strtok function. More...

#include <string.h>
Include dependency graph for strtok.c:

Go to the source code of this file.

Functions

char * strtok (char *s1, const char *s2)
 Breaks a string into a sequence of tokens split on a given delimiter. More...
 

Detailed Description

Defines the strtok function.

Definition in file strtok.c.

Function Documentation

◆ strtok()

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

Breaks a string into a sequence of tokens split on a given delimiter.

Parameters
*s1String to be tokenized
*s2Separator string used to delimit tokens

A sequence of calls to the strtok functions 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 separator string pointed to by s2 may be different from call to call.

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 strtok function returns a null pointer. If such a character is found, it is the start of the first token.

The strtok function 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. The strtok function 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.

The implementation shall behave as if no library function calls the strtok function.

Returns
A pointer to the first character of a token
Return values
NULLNo token was found

Definition at line 77 of file strtok.c.

References NULL, strcspn(), and strspn().

Here is the call graph for this function: