【发布时间】:2013-10-29 18:04:03
【问题描述】:
我需要将令牌存储到一个数组中。然后我想在句子中找到相等的标记。
int main()
{
int i=0;
char* words[200];
char text[200];
printf("Enter one sentence \n ");
gets(text);
char *word = strtok(text, " ");
while(word!=0)
{
words=malloc(strlen(word)+1);
strcpy(words[i++], word);
printf("[%s]\n", word);
word=strtok(NULL, " ,.!?");
}
getch();
return 0;
}
我不知道为什么这个 errr- 将 void* 分配给 char*[200]' 时出现 22 种不兼容的类型 如果我将 words 更改为 words[i]=malloc ..... 得到错误 22 从 void* 到 char* 的无效转换'
然后我想知道如何从这个数组中将这些标记与 strcmp 进行比较。 这是我的尝试,但看起来不起作用。
for (k=0; k<199; k++)
{
for (j=k+1; j<200; j++)
{
if (strcmp(words[k],words[j])==0)
{
printf("equal words are %d",words);
}
else
{
printf("In this sentence aren't equal words");
}
}
}
【问题讨论】:
-
我建议“words[i++] = strdup(word);”而不是 malloc 和 strcpy。