The use of strtok function in C

#include <stdio.h>
#include <string.h>

int test01()
{
    
    
    char a[100] = "abc_123_def_564_ddddddd_qqqqqq_xxxxx";
    char *s;//定义一个char的指针变量
    s = strtok(a, "_");
    printf("%s\n", s);
    s = strtok(NULL, "_");//第二次调用的时候,第一个参数写NULL
    printf("%s\n", s);
    s = strtok(NULL, "_");
    printf("%s\n", s);
    s = strtok(NULL, "_");

    printf("%s\n", s);
    s = strtok(NULL, "_");
    printf("%s\n", s);
    
    return 0;

}

int main(int argc, const char *argv[])
{
    
    
    char a[100] = "abc_123_def_564_ddddddd_qqqqqq_xxxxx";
    char *s     = NULL;
    
    s = strtok(a, "_");
    while(s)
    {
    
    
        printf("%s\n", s);
        s = strtok(NULL, "_");
    }
    
    puts("-------------------------------------------------");

    test01();

    return 0;
    
}


Test Results

Insert picture description here

Guess you like

Origin blog.csdn.net/zxy131072/article/details/108489305