Name
strtok
Synopsis
Divides a string into tokens
#include <string.h> char *strtok
( char * restricts1
, const char * restricts2
);
The strtok()
function
isolates tokens in the string addressed by
s1
that are delimited by any of the
characters contained in the string addressed by
s2
. The tokens are identified one at a
time by successive calls to strtok()
. On calls after the first, the
s1
argument is a null pointer.
On the first call, strtok()
searches in s1
for the first character
that does not match any character in s2
,
behavior that is similar to the strspn()
function. The first such
character found is considered to be the beginning of a token. Then
strtok()
searches further for the
first character that does match any of the
characters in s2
—or the null character
that terminates the string, whichever comes first—similarly to the
strcspn()
function. This is
considered to be the delimiter that ends the token. strtok()
then replaces this ending
delimiter with '\0'
, and returns
a pointer to the beginning of the token (or a null pointer if no
token was found), while saving an internal, static pointer to the
next character after the ending delimiter for use in subsequent
strtok()
calls.
On each subsequent call with a null pointer as the
s1
argument, strtok()
behaves similarly, but starts the
search at the character that follows the previous delimiter. You can
specify a different set of delimiters in the
s2
argument on each call. The locations
that strtok()
reads from using
s2
and writes to ...
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.