Extract tokens from a string
String_Type[] strtok (String_Type str [,String_Type white])
strtok breaks the string str into a series of tokens and
returns them as an array of strings. If the second parameter
white is present, then it specifies the set of characters that
are to be regarded as whitespace when extracting the tokens, and may
consist of the whitespace characters or a range of such characters.
If the first character of white is '^', then the
whitespace characters consist of all characters except those in
white. For example, if white is " \t\n,;.",
then those characters specifiy the whitespace characters. However,
if white is given by "^a-zA-Z0-9_", then any character
is a whitespace character except those in the ranges a-z,
A-Z, 0-9, and the underscore character.
If the second parameter is not present, then it defaults to
" \t\r\n\f".
The following example may be used to count the words in a text file:
define count_words (file)
{
variable fp, line, count;
fp = fopen (file, "r");
if (fp == NULL) return -1;
count = 0;
while (-1 != fgets (&line, fp))
{
line = strtok (line, "^a-zA-Z");
count += length (line);
}
() = fclose (fp);
return count;
}
|