Dictionary does not return all possible words

Asked

Viewed 218 times

0

The idea of the program is to be a dictionary, the user informs the word, the program performs a check on the characters of the word finds the position in the vector and returns the definition if the word is in the dictionary.

The problem is q it never returns error of word a found, only the first definition or the second :/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> //lider com variaveis booleans
#define NUM_DEF 7

struct dicionario
{
    char palavra[20];
    char definicao[50];
};

//confere os caracteres das palavras
bool compString(const char palavra1[], const char palavra2[])
{
    int i = 0;
    while (palavra1[i] == palavra2[i] && palavra1[i] != '\0' && palavra2[i] != '\0')
    {
        ++i;
    }
    if (palavra1[i] == '\0' && palavra2[i] == '\0')
    {
        return true;
    }
    else
    {
        return false;
    }
}

//busca a palavra no dicionario
int procString(const struct dicionario lingua[], const char procurar[], int numPalavra)
{
    bool compString(const char palavra1[], const char palavra2[]);
    int i = 0;
    while (i < numPalavra)
    {
        if (compString(procurar, lingua[i].palavra))
        {
            return i;
        }
        else
        {
            return ++i;
        }
    }
    return -1;
}

int main(void)
{
    int procString(const struct dicionario lingua[], const char procurar[], const int numPalavra);

    char palavra[20] = {'\0'};
    int resultPesq;

    const struct dicionario portu[NUM_DEF] = {
        {"pao", "comida de farinha"},
        {"feijao", "comida brasileira"},
        {"tropeiro", "tipo de feijao"},
        {"queijo", "comida de minas"},
        {"macarrao", "comida de vo"},
        {"mortadela", "comida de carne"},
        {"pizza", "comida da italia"}};

    printf("Digite uma palavra:\n");
    scanf("%s", &palavra);

    resultPesq = procString(portu, palavra, NUM_DEF);

    if (resultPesq != -1)
    {
        printf("%s\n", portu[resultPesq].definicao);
    }
    else
    {
        printf("Palavra n encontrada");
    }

    return 0;
}

3 answers

3

The main problem is in the function that compares words, the procString:

int procString (const struct dicionario lingua[],const char 
procurar[], int numPalavra){ //busca a palavra no dicionario

    bool compString( const char palavra1[],  const char palavra2[]);
    //^---------esta definição é desnecessária

    int i=0;
    while (i<numPalavra){

        if(compString(procurar,lingua[i].palavra)){
            return i;
        }else{
            return ++i;
        }
        //^---- if e else com return

    }
    return -1;
}

Whenever you have a bond with a return in the if and in the else then the loop only executes once, which makes it meaningless. For the type of logic that is using the for is usually the most appropriate.

Well see how it would be correcting the problem and exchanging for a for:

int procString (const struct dicionario lingua[],const char procurar[], int numPalavra)  //busca a palavra no dicionario
{
    int i=0;
    for (i=0;i < numPalavra; ++i){
        if(compString(procurar,lingua[i].palavra))
        {
            return i;
        }
    }

    return -1;
}

In the main the reading of palavra It’s not right here either:

char palavra[20] = {'\0'};
...
scanf("%s",&palavra /*<--aqui*/);

palavra is actually a pointer to the first letter of the string, right in the scanf does not take the &:

scanf("%s",palavra);

And if you’re gonna read to palavra with a scanf it is unnecessary to put {0} as a starting value.

See the example of searching for a word that does not exist in Ideone

Note: I don’t know if I was trying to implement word comparison as an exercise, but in <string.h> already has the function strcmp who does this to you.

3

The biggest problem there is that you’re not comparing the strings correctly. Need to use function strcmp() to compare character to character. Maybe because you didn’t know it the algorithm got confused doing unnecessary things.

What he should do is just walk the array of words and compare with the words typed, finding return apposition to pick up the definition, if you go through all and do not find returns -1.

As there is a constant definition of the number of elements of the array maybe you can delete a function parameter that passes this number, at least for this case I think it looks better.

There were other errors solved, for example in the scanf()was passed a pointer to a array. A array is already a pointer, then passes the very array

I took away all that was unnecessary.

#include <stdio.h>
#include <string.h>
#define NUM_DEF 7

struct dicionario {
    char palavra[20];
    char definicao[50];
};

int procString (const struct dicionario lingua[], const char 
procurar[], int numPalavra) {
    for (int i = 0; i < numPalavra; i++) {
        if (strcmp(lingua[i].palavra, procurar) == 0) {
            return i;
        }
    }
    return -1;
}

int main(void) {
    const struct dicionario portu[NUM_DEF] = {
        {"pao","comida de farinha"},
        {"feijao","comida brasileira"},
        {"tropeiro","tipo de feijao"},
        {"queijo","comida de minas"},
        {"macarrao","comida de vo"},
        {"mortadela","comida de carne"},
        {"pizza","comida da italia"}};
    printf("Digite uma palavra:\n");
    char palavra[20] = {'\0'};
    scanf("%s", palavra);
    int resultadoPesquisa = procString(portu, palavra, NUM_DEF);
    if (resultadoPesquisa != -1) {
        printf("%s\n", portu[resultadoPesquisa].definicao);
    } else {
        printf("Palavra n encontrada");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1


If the intention is to write your own comparison function of strings avoiding the use of family functions strcmp(), I suggest you modify your function compString() for something like:

bool compString( const char palavra1[], const char palavra2[] )
{
    int i = 0;

    for( i = 0; ; i++ ) {
        if (palavra1[i] != palavra2[i])
            return false;
        if (palavra1[i] == '\0')
            return true;
    }
} 

Testing:

#include <stdio.h>
#include <stdbool.h>

#define NUM_DEF 7

struct dicionario {
    char palavra[20];
    char definicao[50];
};

bool compString( const char palavra1[], const char palavra2[] )
{
    int i = 0;

    for( i = 0; ; i++ ) {
        if (palavra1[i] != palavra2[i])
            return false;
        if (palavra1[i] == '\0')
            return true;
    }
}

int procString( const struct dicionario dic[], int numPalavra, const char procurar[] )
{
    int i =0;

    for(i = 0; i < numPalavra; i++ )
        if(compString( dic[i].palavra, procurar ))
            return i;

    return -1;
}

int main(void)
{
    char palavra[20] = {0};
    int resultPesq;

    const struct dicionario portu[NUM_DEF] =
        {{"pao","comida de farinha"},
        {"feijao","comida brasileira"},
        {"tropeiro","tipo de feijao"},
        {"queijo","comida de minas"},
        {"macarrao","comida de vo"},
        {"mortadela","comida de carne"},
        {"pizza","comida da italia"}};

    printf("Digite uma palavra: ");
    scanf("%s", palavra );

    resultPesq = procString( portu, NUM_DEF, palavra );

    if( resultPesq == -1 ){
        printf("Palavra nao encontrada!\n");
        return 1;
    }

    printf("%s\n", portu[resultPesq].definicao );

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.