String segmentation in C Language -- strtok

Source: Internet
Author: User
Tags strtok

Previously, we had to implement string splitting by ourselves. Recently, we found that the c standard library already provides this function. I am still not familiar with the standard library, which leads to repeated wheel creation. It is necessary to take a look at the standard library...
Let's take a look at the previously implemented string segmentation function:
[Cpp]
Void strsplit (const char * str, char * delim, void (* visitor) (const char *, int, int )){
Int I, k_s, k_e, s_len, d_len;
S_len = strlen (str );
D_len = strlen (delim );
 
Assert (str! = NULL & delim! = NULL & visitor! = NULL );

K_s = k_e = 0;
While (k_e <s_len ){
If (k_e + d_len <= s_len) & strncmp (str + k_e, delim, d_len) = 0 ){
If (k_e> k_s)
Visitor (str, k_s, k_e );
K_s = k_e + d_len;
} Else {
K_e ++;
}
}
 
If (k_e> k_s ){
Visitor (str, k_s, k_e );
}
}
Str: string of the string to be split.
Delim: delimiter. It can be a string of multiple characters.
Visitor: A function pointer that is called when a substring is identified.
For the string "abc #123 # a #", the separator is "#", which is divided into "abc", "123", and "# ". That is to say, the substring in the string to be split must be equal to the split string to be considered as a separator, rather than being included in the split string as a separator, which is a little different from the standard library. However, if the delimiter has only one character, it is the same as the execution result of the standard library. For example, if the separator of the above string is "#", the final result is "abc ", "123", "".
This inconsistency occurs because if a substring is equal to the split string, strlen (split string) characters are skipped and the check continues. If you skip to the next character and continue the check, the result will be the same as that of the standard library function. Replace row 13th:
[Cpp]
K_e ++;
K_s = k_e + (d_len-1 );
The preceding section describes how to split strings. Next, let's take a look at the strtok function provided by the standard library.
[Cpp]
Char * strtok (char * str, const char * delim );
Str: string to be separated.
Delim: delimiter. All characters contained in the delimiter are discarded as delimiter.
Let's look at an example:
[Cpp]
# Include <string. h>
# Include <stdio. h>
# Include <stdlib. h>
 
Int
Main (int argc, char ** argv ){
Char * s, * d, * t;
 
If (argc <3 ){
Printf ("% s \ n", "wrong args. usage: str delim ");
Exit (1 );
}
 
S = argv [1];
D = argv [2];
 
T = strtok (s, d );
While (t! = NULL ){
Printf ("% s \ n", t );
T = strtok (NULL, d );
}
}
To call strtok for the first time, you need to input the str string to be split for initialization. Then, you can continue to split the same string and pass in NULL. Note that if NULL is not input, an endless loop will occur and the first split substring of str is always returned.
The delim character string can be divided into different strings each time. That is to say, strings can be separated by different separators. However, this requirement is not commonly used.
After using this function, it is easier to parse the configuration file, so you do not need to be disturbed by various bugs.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.