Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C strtok()在嵌套时只标记一次_C_String_Strtok - Fatal编程技术网

C strtok()在嵌套时只标记一次

C strtok()在嵌套时只标记一次,c,string,strtok,C,String,Strtok,假设我有以下字符串:0:1,2,3 我想使用:作为分隔符来分隔第一部分,当它到达第二部分(即1,2,3)时,尝试使用strtok(使用,)来表示它没有按预期工作 #include <stdio.h> #include <stdbool.h> int main(void){ char s[10]; strcpy(s, "0:1,2,3"); char* token1 = strtok(s, ":"); //To let me know it

假设我有以下字符串:
0:1,2,3

我想使用
作为分隔符来分隔第一部分,当它到达第二部分(即
1,2,3
)时,尝试使用
strtok
(使用
)来表示它没有按预期工作

#include <stdio.h>
#include <stdbool.h>

int main(void){
    char s[10];
    strcpy(s, "0:1,2,3");
    char* token1 = strtok(s, ":");
    //To let me know it is on the second part
    bool isSecondToken = false;
    while (token1) {
        printf("token1: %s\n", token1);
        if(isSecondToken == true){
            char* token2 = strtok(token1, ",");
            while (token2) {
                printf("token2: %s\n", token2);
                token2 = strtok(NULL, " ");
            }
        }
        token1 = strtok(NULL, " ");
        isSecondToken = true;
    }
}
预期产出:

token1: 0
token1: 1,2,3
token2: 1
token2: 2
token2: 3

更新
token1
token2
指针时,需要使用相同的令牌拆分器:

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

int main(void){
    char s[10];
    strcpy(s, "0:1,2,3");
    char* token1 = strtok(s, ":");
    //To let me know it is on the second part
    bool isSecondToken = false;
    while (token1) {
        printf("token1: %s\n", token1);
        if(isSecondToken == true){
            char* token2 = strtok(token1, ",");
            while (token2) {
                printf("token2: %s\n", token2);
                token2 = strtok(NULL, ",");
            }
        }
        token1 = strtok(NULL, ":");
        isSecondToken = true;
    }
}
#包括
#包括
#包括
内部主(空){
chars[10];
strcpy(s,“0:1,2,3”);
char*token1=strtok(s,“:”);
//让我知道它在第二部分
bool isSecondToken=false;
while(token1){
printf(“令牌1:%s\n”,令牌1);
如果(isSecondToken==true){
char*token2=strtok(token1,“,”);
while(token2){
printf(“令牌2:%s\n”,令牌2);
token2=strtok(NULL,“”,“”);
}
}
token1=strtok(NULL,“:”);
isSecondToken=true;
}
}

另外,
strcpy
需要
string.h
库,因此您可能也会收到一些隐式声明的警告。

在更新
token1
token2
指针时,您需要使用相同的令牌拆分器:

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

int main(void){
    char s[10];
    strcpy(s, "0:1,2,3");
    char* token1 = strtok(s, ":");
    //To let me know it is on the second part
    bool isSecondToken = false;
    while (token1) {
        printf("token1: %s\n", token1);
        if(isSecondToken == true){
            char* token2 = strtok(token1, ",");
            while (token2) {
                printf("token2: %s\n", token2);
                token2 = strtok(NULL, ",");
            }
        }
        token1 = strtok(NULL, ":");
        isSecondToken = true;
    }
}
#包括
#包括
#包括
内部主(空){
chars[10];
strcpy(s,“0:1,2,3”);
char*token1=strtok(s,“:”);
//让我知道它在第二部分
bool isSecondToken=false;
while(token1){
printf(“令牌1:%s\n”,令牌1);
如果(isSecondToken==true){
char*token2=strtok(token1,“,”);
while(token2){
printf(“令牌2:%s\n”,令牌2);
token2=strtok(NULL,“”,“”);
}
}
token1=strtok(NULL,“:”);
isSecondToken=true;
}
}

另外,
strcpy
需要
string.h
库,因此您可能也会收到一些隐式声明的警告。

您在循环中对strtok的调用表示您希望按空格分割,而不是按逗号分割,因此它将返回,直到找到下一个从未找到的空格,因此为“2,3”在循环中对strtok的调用表示要按空格而不是逗号分割,因此它将返回,直到找到下一个从未找到的空格,因此为“2,3”