strtok() Finds Next Token in String
#include <string.h> Required for declarations only
char *strtok(string1,string2);
char *string1; String containing tokens
char *string2; Delimiter characters
strtok() considers 'string1' to be a sequence of zero or more tokens,
each separated by one or more delimiters. 'string2' is the set of
characters serving as delimiters of the tokens in 'string1'.
The tokens in 'string1' are extracted by a series of calls to
strtok(). In the first call to strtok() with a given 'string1',
strtok() will search for the first token, skipping leading
delimiters, and return a pointer to the first token. To read the
next token, call strtok() with a NULL 'string1', which will cause
strtok() to continue searching for the next token in the previous
string. You can change 'string2' between calls.
Returns: A pointer to the first token in 'string1' is returned on
the first call to strtok(). In subsequent calls with the
same 'string1' (indicated by a NULL argument for
'string1'), strtok() returns a pointer to the next token
in the string. A NULL pointer is returned when there are
no more tokens.
Notes: strtok() modifies 'string1'; each time it is called, it
inserts a NULL value after the token found.
-------------------------------- Example ---------------------------------
The following statements retrieve and print tokens from 'str1'.
#include <string.h>
#include <stdio.h>
char *str1 = "apples, oranges, bananas, pears.";
char *delim = " ,.";
char *tokens;
main()
{
while((tokens = strtok(str1,delim)) != NULL)
printf("%s\n",tokens);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster