它不完整,但它没有打印任何内容
char *fruit[] = {"rasberry", "cherry"};
char *veg[] = {"salad", "tomato"};
char word;
printf("Print a veg or a fruit \n");
scanf("%s", &word);
for (int i = 0; i < strcmp(fruit, veg ); i++)
{
if (word == fruit[i])
printf("Its fruit!");
}
如何比较?
最佳答案
好吧,在进一步澄清需要什么之后,尝试这样的事情:
char *fruit[] = {"rasberry", "cherry"};
char *veg[] = {"salad", "tomato"};
char word[100]={0};
printf("Print a veg or a fruit \n");
scanf("%s", word);
// Check fruit
for (int i = 0; i < 2; i++)
{
if (strcmp(word,fruit[i])==0)
{
printf("Its fruit \n");
}
}
// Check veg
for (int i = 0; i < 2; i++)
{
if (strcmp(word,veg[i])==0)
{
printf("Its veg \n");
}
}
关于C :How to compare two strings and find a word i need?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26682626/