strtok
---get next token from a string#include <string.h> char *strtok(char *source, const char *delimiters) char *_strtok_r(void *reent, const char *source, const char *delimiters)Description
strtok
break the string starting at
*source
into a sequence of tokens. The tokens are
delimited from one another by characters from the string at
*delimiters
, at the outset.
The first call to strtok
normally has a string address as
the first argument; subsequent calls can use NULL
as the
first argument, to continue searching the same string. You
can continue searching a single string with different
delimiters by using a different delimiter string on each call.
strtok
begins by searching for any character not in the
delimiters string: the first such character is the
beginning of a token (and its address will be the result of
the strtok
call). strtok
then continues searching
until it finds another delimiter character; it replaces that
character by NULL
and returns. (If strtok
comes to
the end of the *source
string without finding any more
delimiters, the entire remainder of the string is treated as
the next token).
strtok
starts its search at *source
, unless you
pass NULL
as the first argument; if source is
NULL
, strtok
continues searching from the end of the
last search. Exploiting the NULL
first argument leads to
non-reentrant code. You can easily circumvent this problem by
saving the last delimiter address in your application, and
always using it to pass a non-null source argument.
_strtok_r
performs the same function as strtok
, but
is reentrant. The extra argument reent is a pointer to
a reentrancy structure.
Returns
strtok
returns a pointer to the next token, or NULL
if
no more tokens can be found.
Portability
strtok
is ANSI C.
strtok
requires no supporting OS subroutines.