Extract tokens within a character array.
#include <string.h> char *strtok(char *str, const char *sep);
str
A pointer to a character string to separate into tokens.
sep
A pointer to a character string containing separator characters.
This function divides a character array pointed to by str into separate tokens. The sep argument points to a character array containing one or more separator characters. The tokens in str are extracted by successive calls to strtok().
The function works by a sequence of calls. The first call is made with the string to be divided into tokens as the first argument. Subsequent calls use NULL as the first argument and returns pointers to successive tokens of the separated string.
The first call to strtok() causes it to search for the first character in str that does not occur in sep. If no character other than those in the separator string can be found, the function returns a null pointer ( NULL). If no characters from the separator string are found, the function returns a pointer to the original string. Otherwise the function returns a pointer to the beginning of this first token.
Make subsequent calls to strtok() with a NULL str argument, causing it to return pointers to successive tokens in the original str character array. If no further tokens exist, strtok() returns a null pointer.
Both str and sep must be null terminated character arrays.
The sep argument can be different for each call to strtok(). The function modifies the character array pointed to by str.
This facility may not be available on some configurations of the EWL.
#include <string.h> #include <stdio.h> int main(void) { char s[50] = "(ape+bear)*(cat+dog)"; char *token; char *separator = "()+*"; /* First call to strtok(). */ token = strtok(s, separator); while(token != NULL) { puts(token); token = strtok(NULL, separator); } return 0; } Output: ape bear cat dog