如何使用2个分隔符剪切字符串

时间:2013-08-31 13:22:53

标签: c string delimiter

如何在C中使用2个分隔符剪切字符串?

我从这个平台的用户那里得到一个字符串:

cp <path1> <path2>

我需要将pathes变成一个新的字符串(每个路径到一个字符串)。

我尝试使用strstrstrtok,但它不起作用。

我不知道pathes的长度。我也知道他们是从" \"开始的(这是我的分隔符(space + \))。

这是我试过的     #包括     #包括     #include

int main()
{
  char *c;
  char *ch = malloc(1024);
  while (strcmp(ch, "exit"))
  {
    scanf("%[^\n]%*c", ch); //what was the input (cp /dor/arthur /king/apple)
    c = malloc(sizeof(strlen(ch) + 1));
    strcpy(c, ch);
    char *pch = strtok(c, " //");
    printf("this is : %s \n", pch); //printed "this is: cp"
  }
}

2 个答案:

答案 0 :(得分:1)

使用strtok()。上面的链接包含使用strtok().

的示例

您可以通过这种方式使用space + \的2个分界符(strtok()):

str = strtok(str, " \\");

答案 1 :(得分:-1)

是主要功能吗?如果是,主函数有argc(int)和* argv [](字符串)参数,你可以做你想做的事。

相关问题