鱼C论坛

 找回密码
 立即注册
查看: 1308|回复: 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.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

你可以将这些标记放在一个字符串中,然后将该字符串作为strtok的第二个参数传递。在分割时,strtok将忽略该字符串中包含的任何字符。以下是一个示例代码:

  1. char str[] = "input.txt < output.txt";
  2. char *token;
  3. char *input_file;
  4. char *output_file;
  5. char delimiters[] = "<> ";

  6. token = strtok(str, delimiters);
  7. while(token != NULL){
  8.     if(input_file == NULL){
  9.         input_file = token;
  10.     }else{
  11.         output_file = token;
  12.     }
  13.     token = strtok(NULL, delimiters);
  14. }
复制代码


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

使用道具 举报

发表于 2023-9-27 23:25:01 | 显示全部楼层
are you chinese or englishman or american
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

我是中国人。我只是把喂给chatgpt的问题复制到这再问一遍。你知道的,问chatgpt还是问英文比较好。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 10:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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