-1

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

2 Answers2

0

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;
    }
Yoram
  • 572
  • 6
  • 21
0
#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;
}

Also see: How do I properly compare strings?

Community
  • 1
  • 1
BUCH
  • 103
  • 4
  • `printf("The word isnt in the array. %s\n", word_array[i], search);` : arguments mismatch. and move to outside of for-loop. – BLUEPIXY Nov 22 '16 at 22:48
  • @BLUEPIXY Depends on what he wants to do with that line(isn't the prettiest that's for sure). However, moving it outside of the loop in it's current form won't do any good. – BUCH Nov 22 '16 at 22:56
  • Thanks for the answer :) – Enes Körhan Nov 23 '16 at 08:28