C中 strtok函数的使用

#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;
    
}


测试结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zxy131072/article/details/108489305