C :How to compare two strings and find a word i need?

标签 c loops compare strcmp

它不完整,但它没有打印任何内容

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/

相关文章:

c - Gstreamer 中的图片幻灯片

r - 如何替换数组中的值?

java - 跳过 Java 字符串中的二元语法(比较两个句子)

java-8 - Comparator.comparing() 函数如何工作?

Java JNI C 程序适用于 Mingw32,但不适用于 Cygwin64

c - C 中的 exec 系列函数

c - nginx c 模块 hello world 不起作用

python - 这两个 python for 循环是一回事吗?

python - 为什么我的程序没有显示我告诉它的碰撞框?

c - 比较字符串和包含土耳其字符的用户输入时,strcmp 函数不起作用