Enes Körhan Enes Körhan - 16 days ago 4
C Question

To check if two words are equal to each other?(C)

Hello.I want to make a simple English-Turkish dictionary. This is my homework.I have to code with C but I don't know C at all. Why doesn't my following code work?

#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;
}

Answer

Review the code below. It fixed few issues with your code.

    #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;
    }