鱼C论坛

 找回密码
 立即注册
查看: 978|回复: 3

split the string

[复制链接]
发表于 2023-9-26 20:05:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
If I want to split the string using strtok, but I want to split by tokens either "<" or ">" or " ", how should I pass the parameter into strtok?
Also, I want to put the part right after "<" but before other tokens into char * input_file, the part right after "<" but before other tokens into char* output_file.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-26 20:05:45 | 显示全部楼层
本帖最后由 isdkz 于 2023-9-26 20:08 编辑

你可以将这些标记放在一个字符串中,然后将该字符串作为strtok的第二个参数传递。在分割时,strtok将忽略该字符串中包含的任何字符。以下是一个示例代码:
char str[] = "input.txt < output.txt";
char *token;
char *input_file;
char *output_file;
char delimiters[] = "<> ";

token = strtok(str, delimiters);
while(token != NULL){
    if(input_file == NULL){
        input_file = token;
    }else{
        output_file = token;
    }
    token = strtok(NULL, delimiters);
}

在上面的代码中,我们将输入字符串存储在str数组中,然后将"<> "存储在delimiters数组中。然后,我们使用strtok函数将字符串分割成单个标记,并使用input_file和output_file指针将各个标记存储在不同的变量中。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-27 23:25:01 | 显示全部楼层
are you chinese or englishman or american
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-29 09:32:31 | 显示全部楼层
额外减小 发表于 2023-9-27 23:25
are you chinese or englishman or american

我是中国人。我只是把喂给chatgpt的问题复制到这再问一遍。你知道的,问chatgpt还是问英文比较好。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 01:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表