Strtok Split String function, a good solution to the requirements of character segmentation, do not have to traverse the keyword after the block character
So it's much easier to find the key in the string and get what's behind it.
#include <string.h>
Plus, like a header file.
char * strtok (char * str, const char * delimiters);
Parameter meaning
STR:: The first operation when the original string, when Strtok split once succeeded, set to NULL to continue scanning the following characters know to be empty
Delimiters :: tag character Xiaowan#Xiaoli symbol #
A simple example is as follows
/ * Strtok Example * /#include <stdio.h>#include <string.h>intMain () {charStr[] ="- 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," ,.-");//Here to success once, set to empty, continue scanning}return0;}
|
Edit & Run |
Run results
Splitting string "- This, a sample string." into tokens:Thisasamplestring
Based on the analysis of the results
String chant. ",--" These three characters are split up
Return ValueIf A token is found, a pointer to the beginning of the token.
Otherwise, a
NULL pointer.
A
NULL pointeris all returned when the end of the string (i.e., a null character) was reached in the string being Scanne D.
The magical function of Strtok, splitting the string