How can I input a word, read through a dictionary, and print the word's translation?

74 views Asked by At

My objective is to input a string "sana" which will at the end print only one corresponding result. Ie: if I enter the word "sana" it would print "Word sana is in English word" and if the user enters "word" it prints "Word word is in Finnish sana". So the code is not working at all so I wanted to ask how should I continue. if-else doesn't work at all but I thought it would help me visualize where to go there.

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

/*
Read file line by line
split lines using ; as delimiter
store first/second part in some array
ask user to input and search the word in that array
*/

int main()
{
    FILE *fp;
    char sana[30];
    char *p;
    void *tmp;
    int lasku = 0;
    int pal = 1;
    int i;
    char **vars = NULL;
    char **vals = NULL;

    fp = fopen("dictionary.txt", "r");

    printf("Word: ");
    scanf("%s", sana);

    while (fgets(sana, sizeof(sana), fp)) {
        sana[strcspn(sana, "\n")] = 0;
        if (!(p = strchr(sana, ';')))
            continue;
        *p++ = 0;       //poistaa ;

        if (!strlen(sana) || !strlen(p))
            continue;

        if (!(tmp = realloc(vars, (lasku + 1) * sizeof(char*))))
            goto out;
        vars = (char**)tmp;

        if (!(tmp = realloc(vals, (lasku + 1) * sizeof(char*))))
            goto out;
        vals = (char**)tmp;

        vars[lasku] = strdup(sana);
        vals[lasku] = strdup(p);
        lasku++;

        if (!vars[lasku-1] || ! vals[lasku-1])
            goto out;
    }

    pal = 0;

    if (i == 0 || i == 2 || i == 4)
        printf("Word %s is in English %s\n", vars[i], vals[i]);
    else
    if (i == 1 || i == 3 || i == 5)
        printf("Word %s is in Finnish %s\n", vals[i], vars[i]);
    else
        printf("Word can't be found in the dictionary");

  out:
    fclose(fp);

    if (vars)
        for (i = 0; i < lasku; i++)
            free(vars[i]);
    if (vals)
        for (i = 0; i < lasku; i++)
            free(vals[i]);
    free(vars);
    free(vals);

    return pal;
}
1

There are 1 answers

3
chqrlie On

The code does not work because:

  • you overwrite the word in sana when you read the dictionary.
  • you never set i.
  • testing the value of uninitialized variable i has undefined behavior.

You should first local the dictionary in memory, then read words from the user and search them in the dictionary for matches.

Here is a modified version:

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

/*
Read file line by line
split lines using ; as delimiter
store first/second part in some array
ask user to input and search the word in that array
*/

int main() {
    FILE *fp;
    char sana[30];
    char **tmp;
    int lasku = 0, i, found, error = 0;
    char **vars = NULL;
    char **vals = NULL;

    fp = fopen("dictionary.txt", "r");

    while (fgets(sana, sizeof(sana), fp)) {
        sana[strcspn(sana, "\n")] = '\0';
        if (!(p = strchr(sana, ';')))
            continue;
        *p++ = '\0';
        if (!*sana || !*p)
            continue;

        if (!(tmp = realloc(vars, (lasku + 1) * sizeof(*vars)))) {
            error = 1;
            break;
        }
        vars = tmp;

        if (!(tmp = realloc(vals, (lasku + 1) * sizeof(*vals)))) {
            error = 1;
            break;
        }
        vals = tmp;

        vars[lasku] = strdup(sana);
        vals[lasku] = strdup(p);
        lasku++;

        if (!vars[lasku-1] || !vals[lasku-1]) {
            error = 1;
            break;
        }
    }
    fclose(fp);

    if (!error) {
        for (;;) {
            printf("Word: ");
            if (scanf("%29s", sana) != 1)
                break;
            found = 0;
            for (i = 0; i < lasku; i++) {
                if (!strcmp(sana, vars[i]) {
                    printf("Word %s is in English %s\n", vars[i], vals[i]);
                    found = 1;
                }
                if (!strcmp(sana, vals[i]) {
                    printf("Word %s is in Finnish %s\n", vals[i], vars[i]);
                    found = 1;
                }
            }
            if (!found) {
                printf("Word can't be found in the dictionary");
            }
        }
    }

    for (i = 0; i < lasku; i++) {
        free(vars[i]);
        free(vals[i]);
    }
    free(vars);
    free(vals);

    return error;
}