我正在尝试将一个句子标记为仅由单个空格分隔的单词。我需要从句子中获取单词,然后将它们写入订阅数组。这是我的工作:
void writeToStrArrayOneByOne(char words[10][20], char *sentence){
char *tokenPtr;
int j = 0;
int a,i;
tokenPtr = strtok(sentence," ");
while(tokenPtr != NULL){
a = strlen(tokenPtr);
for(i=0;i<a;i++){
words[j][i] = tokenPtr[i];
}
tokenPtr = strtok(NULL," ");
j++;
}
我只用这个来调用函数,以便从 main 进行调试:
char words[10][20];
char *sentence = "this is a token";
writeToStrArrayOneByOne(words,sentence);
但是,它像 EOF 异常时一样冻结。感谢任何帮助,谢谢。