C-language String segmentation--strtok

Source: Internet
Author: User
Tags strlen strtok

Before encountered the need for string segmentation, are to implement their own, recently found that the C standard library has provided this function. The standard library is still unfamiliar ah, resulting in the repetition of the wheel, it is necessary to look at the standard library ...

Let's take a look at the string-splitting function you've implemented before:

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 = K_e + D_len;
                } else{
                        k_e++
                }
        }

        if (K_e > k_s) {
                visitor (str, k_s, k_e);
        }

STR: A string of strings to be split.

Delim: Delimiter, can be a string of more than one character.

Visitor: function pointer that is invoked when a substring is recognized.
For the string "abc# #123 # # #a # #, the separator is" # # ", it will be divided into" abc "," 123 "," #a ". That is, the substring in the string to be split must be equal to the split string to be a delimiter, rather than the standard library, as long as it is included in the split string, even if it is delimited. But only one character for the delimiter is the same as the result of the standard library: If the string above is "#", then the final result is "abc", "123", "a".

This inconsistency occurs because if a substring is equal to a split string, the Strlen (split string) character is skipped directly, and then the check continues. If this is changed to jump to the next character and then continue checking, it will get the same result as the function of the standard library. is to replace line 13th with the following:

k_e++;
k_s = K_e + (d_len-1);
The above describes the implementation of the split string, the following look at the functions provided by the standard library strtok.

Char *strtok (char *str, const char *delim);
STR: the string to split.

Delim: Splits a string, and the characters contained in the split string are discarded as a split character.

Look at an example:

#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);
        }          
                  
The first call to strtok needs to be initialized by passing in the string str to be split, and then continuing to split incoming null on the same string. This place needs to be noted that if NULL is not passed in, a dead loop will appear, returning the first substring of str.

Split string Delim each time you can pass in a different string, that is, you can split the string according to different delimiters, but such requirements are less common.

It is more convenient to parse the configuration file after this function, without being disturbed by a variety of bugs.



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.