Strtok (): "string segmentation" Utility
Recently I have been entangled in a very simple question: how to separate a string by a flag? The original intention of this problem is to handle the problem of converting the command line string to the argc and argv formats.
I tried many methods and finally thought it would be a good way to use the strtok () function. First, we will introduce the strtok () function.
Char * strtok (string, control );
--- Tokenize string with delimiter in control.
--- Use delimiter control to slice the string.
Purpose:
Strtok considers the string to consist of a sequence of zero or more text tokens separated by spans of one or more control chars. the first call, with string specified, returns a pointer to the first char of the first token, and will write a null char into string immediately following the returned token. subsequent callwith zero for the first argument (string) will work through the string until no tokens remain. the control string may be different from call to call. when no tokens remain in string a NULL pointer is returned. remember the control chars with a bit map, one bit per ASCII char. the null char is always a control char.
Strtok considers a string to be composed of one or more text slices separated by one or more characters. During the first call, the first character pointer of the first text slice is returned, and the control character after the text slice is set to NULL. Subsequent calls repeat the preceding operation until no first character is returned (NULL is returned) for the text slice. These control characters may vary in different calls. If there is no text slice in the string, NULL is returned. In the first call, the string points to the string to be decomposed, and then the string is set to NULL when it is called again. Strtok searches for characters contained in delim in string and replaces them with NULL ('/0') until the entire string is searched.
Entry:
Char * string --- string to tokenize, or NULL to get next token
Char * control --- string of characters to use as delimiters
Exit:
Return pointer to first token in string, or if string was NULL, to next token. Return NULL when no more tokens remain.
Source Code:
# Include
Static char * olds;/* Parse S into tokens separated by characters in DELIM. if S is NULL, the last string strtok () was called with is used. for example: char s [] = "-abc-=-def"; x = strtok (s ,"-"); // x = "abc" x = strtok (NULL, "-="); // x = "def" x = strtok (NULL, "= "); // x = NULL // s = "abc \ 0 =-def \ 0" */char * strtok (s, delim) char * s; const char * delim; {char * token; if (s = NULL) s = olds;/* Scan leading delimiters. * // strspns: returns the subscript of the first character in string s that does not appear in the specified string delim. // moves the pointer to the position of the character in the first non-delim ); if (* s = '\ 0') {olds = s; return NULL;}/* Find the end of the token. */token = s; // char * strpbrk (const char * token, const char * delim); // check characters in string s in sequence, when the character to be tested also contains the character string delim, the test is stopped and the position of the character is returned, NULL is not included. // obtain the position where the character in delim appears for the first time in string s. s = strpbrk (token, delim); if (s = NULL) /* This token finishes the string. */olds = _ rawmemchr (token, '\ 0'); else {/* Terminate the token and make OLDS point past it. */* s = '\ 0'; olds = s + 1;} return token ;}
// The core method of its implementation is static char * olds, which is used to save the string after the previous processing.
Example:
Splits the string "Hello, Brief, Kitty" into "Hello", "Brief", and "Kitty ".
The code can be implemented as follows:
#include
#include
int main(){int i=0;char str[] = "Hello,Brief,Kitty";char *token[3];char *buf = str;while((token[i] = strtok(buf, ",")) != NULL){i++;buf = NULL;}for(i=0;i<3;i++)printf("%s\n",token[i]);return 0;}
Currently, the following code is available for General Command Line Processing:
# Include
# Include
# Define MAXLEN 1024 # define MAXCOUNT 64int main () {int I, argc = 0; char * argv [MAXCOUNT], * buf; char character line [MAXLEN]; printf ("Please input the command line to be parsed: \ n"); if (buf = fgets (queue line, MAXLEN, stdin) = NULL) printf ("input error! \ N "); while (argv [argc] = strtok (buf ,""))! = NULL) {argc ++; buf = NULL;} printf ("There is % d arguments: \ n", argc); for (I = 0; I results in: