Logically, using an array for the pair of English/German words is wrong. If the 
OP is interested, the following code uses a structure instead - I don't know 
whether you have covered these yet.

Also, the individual strings in the translation table are held as pointers 
instead of arrays. Thus I have replaced the empty strings ("") that mark the 
end of the table with null-pointers (NULL), which are easier to test for (and 
take up less storage).

#include <stdio.h>
#include <string.h>

typedef struct
{
    const char *english;
    const char *german;
} Words_t;

static const Words_t words[] =
{
    "dog", "hund",
    "no", "nein",
    "year", "jahr",
    "child", "kind",
    "i", "ich",
    "drive", "fahren",
    "house", "haus",
    "to", "zu",
    NULL, NULL
};

int main(void)
{
    char english[80];
    int  i;

    printf("Enter English word: ");
    gets(english); // should use fgets(), safer

    i = 0;
    while (words[i].english) // test for NULL
    {
        if (!strcmp(english, words[i].english))
        {
            printf("German translation: %s\n", words[i].german);
            break;
        }
        i++;
    }

    if (!words[i].english)
        printf("Not in dictionary\n");

    return 0;
}


Reply via email to