Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查两个单词是否相等?(C)_C - Fatal编程技术网

检查两个单词是否相等?(C)

检查两个单词是否相等?(C),c,C,你好。我想做一本简单的英语-土耳其语词典。这是我的家庭作业。我必须用C编写代码,但我一点也不懂C。为什么我的以下代码不起作用? #include<stdio.h> int main() { int i; char word_array[10][20] ={"araba","car","kalem","pencil","derin","deep","mavi","blue","el","hand" }; //char arama[5] = {'d','e','

你好。我想做一本简单的英语-土耳其语词典。这是我的家庭作业。我必须用C编写代码,但我一点也不懂C。为什么我的以下代码不起作用?

#include<stdio.h>
int main()
{
    int i;

    char word_array[10][20] ={"araba","car","kalem","pencil","derin","deep","mavi","blue","el","hand" };
    //char arama[5] = {'d','e','r','i','n'};
    char search[10] = "araba ";


    for(i = 0 ; i < 10; i=i+2){
        if(word_array[i] == search){
            printf("i found: %s\n",i);
        }
        else{
            printf("The word isnt in the array. %s\n",word_array[i],search);
        }
    }

    return 0;
}
#包括
int main()
{
int i;
char-word_数组[10][20]={“araba”、“car”、“kalem”、“pencil”、“derin”、“deep”、“mavi”、“blue”、“el”、“hand”};
//char arama[5]={'d','e','r','i','n'};
字符搜索[10]=“araba”;
对于(i=0;i<10;i=i+2){
if(单词数组[i]==搜索){
printf(“我发现:%s\n”,i);
}
否则{
printf(“单词不在数组中。%s\n”,单词数组[i],搜索);
}
}
返回0;
}

查看下面的代码。它修复了代码中的一些问题

    #include<stdio.h>
    #include <string.h>
    int main()
    {
        int i;

        char word_array[10][20] ={"araba","car","kalem","pencil","derin","deep","mavi","blue","el","hand" };
        char search[10] = "araba";


        for(i = 0 ; i < 10; i++){
            if(strcmp(word_array[i],search) == 0){
                printf("i found: %s\n",i);
                break;
            }
       }

       if(i>=10) {
            printf("The word %s isnt in the array.\n",search);
        }

        return 0;
    }
#包括
#包括
int main()
{
int i;
char-word_数组[10][20]={“araba”、“car”、“kalem”、“pencil”、“derin”、“deep”、“mavi”、“blue”、“el”、“hand”};
字符搜索[10]=“araba”;
对于(i=0;i<10;i++){
if(strcmp(单词数组[i],搜索)==0){
printf(“我发现:%s\n”,i);
打破
}
}
如果(i>=10){
printf(“单词%s不在数组中。\n”,搜索);
}
返回0;
}
#包括

#包括//不,不包括。使用strcmp
。它也不会,因为在C语言中,
“araba”
的末尾有一个空格,如果不使用
strcpy
strcmp
就不能赋值(除了定义)或比较字符串,这是对的。无法与“if(word_数组[i]==搜索”进行比较。请尝试不同的数据结构,并像其他人所说的那样使用字符串比较。您的代码示例有相当大的错误,可能需要有人重写整个块才能为您提供真正的帮助。在
i=i+2
中,为什么要避免一半字符串?
printf(“我发现:%d\n”,i);
(或
printf(“我找到了:%s\n”,word_数组[i]);
printf(“我找到了:%s\n,word_数组[i+1]);
)谢谢你的回答。这是工作:)
printf(“单词不在数组中。%s\n”,word_数组[i],搜索);
:参数不匹配。并移到for循环的外部。@BLUEPIXY取决于他想用那一行做什么(肯定不是最漂亮的)。但是,以当前形式将其移出循环不会有任何好处。谢谢您的回答:)
#include<stdio.h>
#include <string.h> // <-------- Include header containing strcmp()
int main()
{
    int i;

    char word_array[10][20] = { "araba", "car", "kalem", "pencil", "derin", "deep", "mavi", "blue", "el", "hand" };
    //char arama[5] = {'d','e','r','i','n'};
    char search[10] = "araba"; // <-------- Remove space to have the same string.


    for (i = 0; i < 10; i = i + 1){ // <-------- Search each word in array as opposed to each second word.
        if (strcmp(word_array[i], search) == 0){ // <-------- Use proper C-string comparison.
            printf("%i found: %s\n", i, word_array[i]); // <-------- Correct specifier "i" to "%i" and add the actual found word to output for "%s"
        }
        else{
            printf("The word isnt in the array. %s\n", word_array[i], search);
        }
    }

    return 0;
}