Chapter Contents |
Previous |
Next |
strtok |
Portability: | ISO/ANSI C conforming, UNIX compatible |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
CAUTION | |
EXAMPLE | |
RELATED FUNCTIONS | |
SEE ALSO |
SYNOPSIS |
#include <string.h> char *strtok(char *s1, const char *s2);
DESCRIPTION |
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 tokens are created by a sequence of calls to
strtok
. In the first call in the sequence,
s1
points to the string to be broken down. In
subsequent calls in the sequence,
s1
is
NULL
. The string pointed to by
s2
may be different from call to call.
If
s1
is not
NULL
(that is, the call is the first call in
a sequence), then
strtok
searches the string
pointed to by
s1
for the first character
that is not contained in the string pointed to by
s2
. If such a character is found, then it is the start of the first
token. If no such character is found,
strtok
returns
NULL
. Otherwise, the character
that was found becomes the start of the first token.
After a token has been started,
strtok
searches for the first character contained in the string pointed
to by
s2
. If such a character is found,
it is replaced by
strtok
with a null character,
thereby terminating the token. If no such character is found, the token extends
to the null character terminating the string pointed to by
s1
. In either case,
strtok
returns
the start of the token.
Subsequent calls to
strtok
behave as described in the previous paragraph, starting at the character
following the null character terminating the previous token. However, if
the previous token extended to the end of the string pointed to by
s1
,
strtok
simply returns
NULL
.
RETURN VALUE |
strtok
returns a pointer to the start of a token, or
NULL
if there is no token.
CAUTION |
The pointer that is saved by
strtok
is kept in an
extern
variable
that is local to the calling load module. Therefore, in a multiload module
program, a sequence of calls to
strtok
for a given string must be made from the same load module.
EXAMPLE |
This example using
strtok
breaks out words separated by blanks or commas:
#include <string.h> #include <stdio.h> #include <stddef.h> main() { char test[] = "first,second,third,fourth"; char* token; token = strtok(test, ", "); while(token != NULL){ puts(token); /* Continue scan from where it left off. */ token = strtok(NULL, ", "); } }
RELATED FUNCTIONS |
SEE ALSO |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.