First, header file: #include <string.h>
Second, function prototype: char * strtok (char *str, const char * delimiters);
Three, Parameters: STR, the C-style string to be split (c-string); delimiters, separator string.
Iv. Description:
The string str is decomposed into several words, and the words are split between any one by one characters in the delimiters string. The first time you call Strtok, STR should be a C-style string (c-string), and in subsequent calls, STR should be a null pointer.
V. Examples:
[CPP]View Plaincopy
- /* Strtok Example */
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str[] ="-This, a sample string.";
- char * PCH;
- printf ("splitting string \"%s\ "into tokens:\n", str);
- PCH = Strtok (str,",.-");
- While (pch! = NULL)
- {
- printf ("%s\n", PCH);
- PCH = Strtok (NULL, ",.-");
- }
- return 0;
- }
Output:
[CPP]View Plaincopy
- Splitting string "-This, a sample string." Into tokens:
- This
- A
- Sample
- String
C + + Strtok function