关于strtok函数

发布时间 2023-04-15 00:44:52作者: zhaogaojian

1、该函数参数是char ,所以就算传入一个const char ,也会被修改。
2、替代函数

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

int main() {
    const char *str = "Hello, world! How are you?";
    char *copy = strdup(str);

    char *token = strsep(&copy, " ");

    while (token != NULL) {
        printf("%s\n", token);
        token = strsep(&copy, " ");
    }

    printf("--------------\n%s\n",str);
    
    return 0;
}

注意只支持gnuc,windows下的mingw c不支持的。