Find the next token in a string.
#include <string.h> | Required only for function declarations |
char *strtok( char *string1, const char *string2 );
char __far * __far _fstrtok( char __far *string1, const char __far *string2 );
string1 | String containing token(s) | |
string2 | Set of delimiter characters |
The strtok function reads string1 as a series of zero or more tokens and string2 as the set of characters serving as delimiters of the tokens in string1. The tokens in string1 may be separated by one or more of the delimiters from string2.
The tokens can be broken out of string1 by a series of calls to strtok. In the first call to strtok for string1, strtok searches for the first token in string1, skipping leading delimiters. A pointer to the first token is returned. To read the next token from string1, call strtok with a NULL value for the string1 argument. The NULL string1 argument causes strtok to search for the next token in the previous token string. The set of delimiters may vary from call to call, so string2 can take any value.
The _fstrtok function is a model-independent (large-model) form of the strtok function. The behavior and return value of _fstrtok are identical to those of the model-dependent function strtok, with the exception that the arguments and return value are far pointers.
Note that calls to these functions will modify string1, since each time strtok is called it inserts a null character ('\0') after the token in string1.
The first time strtok is called, it returns a pointer to the first token in string1. In later calls with the same token string, strtok returns a pointer to the next token in the string. A NULL pointer is returned when there are no more tokens. All tokens are null-terminated.
strtok
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_fstrtok
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
/* STRTOK.C: In this program, a loop uses strtok to print all the tokens
* (separated by commas or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens