我注意到我的变量input2只打印字符串中的第一个单词,这导致程序的其余部分出现问题(即不能正确打印名词)。任何有关为什么发生这种情况的见解将不胜感激。printf只打印字符串中的第一个单词?
int main(int argc, char* argv[]){
char *input = strtok(argv[1], " \"\n");
//printf("%s\n", input);
int position;
int check = 0;
int first = 1;
while (input != NULL) {
position = binary_search(verbs, VERBS, input);
//printf("%s\n", input);
//printf("%d\n", position);
if (position != -1){
if (first){
printf("The verbs were:");
first = 0;
check = 1;
}
printf(" %s", input);
}
input = strtok(NULL, " ");
}
if (check == 1){
printf(".\n");
}
if (check == 0){
printf("There were no verbs!\n");
}
char *input2 = strtok(argv[1], " \"\n");
//printf("%s\n", input2);
int position2;
int check2 = 0;
int first2 = 1;
while (input2 != NULL) {
position2 = binary_search(nouns, NOUNS, input2);
//printf("%s\n", input2);
//printf("%d\n", position2);
if (position2 != -1){
if (first2){
printf("The nouns were:");
first2 = 0;
check2 = 1;
}
printf(" %s", input2);
}
input2 = strtok(NULL, " ");
}
if (check2 == 1){
printf(".\n");
}
if (check2 == 0){
printf("There were no nouns!\n");
}
return 0;
}
是不是已经基于空白解析的命令行参数?如果是这样,'argv [1]'从一开始就只有一个单词。 – aardvarkk
@aardvarkk:如果它被称为'程序'fly run think'',那么不是。但要求调用约定并且不支持多个参数是很奇怪的。 – aschepler
@aschepler够了! – aardvarkk