Test equal to the question but is giving error

Asked

Viewed 148 times

0

I decided that 2018 would learn to program, and I’m studying on my own, and by researching I found this judge named Uri, I’m having a hard time finding the error that is in my code, I did all the tests, and when I submit the question of only 10% error someone could help me.

Question link

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

int main(void)
{
 int i, j, vetor[100], c = 0, teste, k;
 char nome[100];
 char primeiro[6] = {'a', 'k', 'u', 'G', 'Q'}, segundo[6] = {'b', 'l', 'v', 
 'I', 'S'};
 char terceiro[7] = {'E', 'O', 'Y', 'c', 'm', 'w'}, quarto[7] = {'F', 'P', 'Z', 'd', 'n', 'x'};
 char quinto[6] = {'J', 'T', 'e', 'o', 'y'}, sexto[7] = {'D', 'N', 'X', 'f', 'p', 'z'};
 char setimo[6] = {'A', 'K', 'U', 'g', 'q'}, oitavo[6] = {'C', 'M', 'W', 'h', 'r'};
 char nono[6] = {'B', 'L', 'V', 'i', 's'}, deimo[5] = {'H', 'R', 'j', 't'};

 scanf("%d", &teste);
 for(k = 0; k < teste; k++)
 {
    setbuf(stdin, NULL);
    fgets(nome, 40, stdin);
    int tam = strlen(nome);
    for(i = 0, c = 0; i < tam; i++)
    {
        for(j = 0; j < strlen(primeiro); j++)
        {
            if(nome[i] == primeiro[j])
            {
                vetor[c++] = 0;
                break;
            }
        }
        for(j = 0; j < strlen(segundo); j++)
        {
            if(nome[i] == segundo[j])
            {
                vetor[c++] = 1;
                break;
            }
        }
        for(j = 0; j < strlen(terceiro); j++)
        {
            if(nome[i] == terceiro[j])
            {
                vetor[c++] = 2;
                break;
            }
        }
        for(j = 0; j < strlen(quarto); j++)
        {
            if(nome[i] == quarto[j])
            {
                vetor[c++] = 3;
                break;
            }
        }
        for(j = 0; j < strlen(quinto); j++)
        {
            if(nome[i] == quinto[j])
            {
                vetor[c++] = 4;
                break;
            }
        }
        for(j = 0; j < strlen(sexto); j++)
        {
            if(nome[i] == sexto[j])
            {
                vetor[c++] = 5;
                break;
            }
        }
        for(j = 0; j < strlen(setimo); j++)
        {
            if(nome[i] == setimo[j])
            {
                vetor[c++] = 6;
                break;
            }
        }
        for(j = 0; j < strlen(oitavo); j++)
        {
            if(nome[i] == oitavo[j])
            {
                vetor[c++] = 7;
                break;
            }
        }
        for(j = 0; j < strlen(nono); j++)
        {
            if(nome[i] == nono[j])
            {
                vetor[c++] = 8;
                break;
            }
        }
        for(j = 0; j < strlen(deimo); j++)
        {
            if(nome[i] == deimo[j])
            {
                vetor[c++] = 9;
                break;
            }
        }
    }
    if(c <= 11)
    {
        for(i = 0; i < c; i++)
        {
            printf("%d", vetor[i]);
        }
        printf("\n");
    }
    else
    {
        for(i = 0; i < 12; i++)
        {
            printf("%d", vetor[i]);
        }
        printf("\n");
    }
    memset(vetor, 0, sizeof(vetor));
}
  system("pause");
  return 0;
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

I am glad that I decided to start learning how to program and started by C. I have bad news: to start doing without really understanding what you are doing doesn’t usually work. And worse, it seems that it worked and the person stays in Dunning-Krugger effect.

The statement is a bit flawed, so I can’t guarantee it will pass. This was my interpretation. And it lacks validation.

#include <stdio.h>

int main(void) {
    char convertido[13];
    char nome[101];
    char *grupos[10] = { "akuGQ", "blvIS", "EOYcmw", "FPZdnx", "JTeoy", "DNXfpz", "AKUgq", "CMWhr", "BLVis", "HRjt" };
    int teste = 3;
    scanf("%d", &teste);
    for (int i = 0; i < teste; i++) {
        setbuf(stdin, NULL);
        fgets(nome, 100, stdin);
        int indiceConvertido = 0;
        for (int posicao = 0; indiceConvertido < 12 && nome[posicao] != '\0' && nome[posicao] != '\n'; posicao++) {
            if (nome[posicao] != ' ') {
                for (int grupo = 0; grupo < 10; grupo++) {
                    for (int item = 0; grupos[grupo][item] != '\0'; item++) { //verifica se ainda não chegou no fim
                        if (nome[posicao] == grupos[grupo][item]) {
                            convertido[indiceConvertido++] = grupo + '0'; //transformando o número em caractere
                            grupo = 10; //para sair dos dois laços
                            break;
                        }
                    }
                }
            }
        }
        convertido[indiceConvertido] = '\0'; //garante que a *string* finaliza após o último caractere
        printf("%s\n", convertido);
    }
}

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

One problem is that its code considered space, and although the statement does not speak anything, it has indications that it should not be considered.

Another problem I considered important is to use one array of integers, I think the intention was to generate characters.

Not that it goes wrong because of this, but the code has individualized something that is collective. Clearly there is a array with groups of letters. This is the array real. It’s a array of strings, and all array of strings has as type char * (pointer to char). Then all logic changes.

I chose not to use the string.h for being an exercise, but could have eliminated a loop using a ready function (which has the loop inside). It is more "C way" to avoid the functions, especially the strlen() which is slow if what you want is not the amount of characters in the string, and in this case is not what you want, you need to know only when the string.

The code is confusing, so it’s even hard to analyze everything. I made it much clearer. If not for you it is because he still lacks knowledge to understand an algorithm of this type and is burning steps and leaving holes in learning.

I think if you have other questions you should ask specific questions since you wanted to know what’s wrong.

0

Follows a possible solution to the problem:

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

#define SAIDA_MAX_TAM    (12)
#define SENHA_MAX_TAM    (20)


char * converter( char * saida, const char * senha )
{
    int i = 0;
    int j = 0;
    int k = 0;
    char * conjuntos[10] = {"GQaku","ISblv","EOYcmw","FPZdnx","JTeoy","DNXfpz","AKUgq","CMWhr","BLVis","HRjt"};

    /* Para cada caractere da senha... */
    for( i = 0; i < strlen(senha) && i < SENHA_MAX_TAM; i++ )
    {
        /* Se o caractere for uma letra... */
        if( isalpha(senha[i]) )
        {
            /* Para cada conjunto... */
            for( j = 0; j < 10 && k < SAIDA_MAX_TAM; j++ )
            {
                /* Se o caracter da senha pertencer ao conjunto... */
                if( strchr( conjuntos[j], senha[i] ) )
                {
                    /* Traduz saida */
                    saida[k++] = '0' + j;
                    break;
                }
            }
        }
    }

    saida[k] = '\0';

    return saida;
}


int main( void )
{
    int i = 0;
    char saida[ SAIDA_MAX_TAM + 1 ];
    char * entrada[] = {
        "Quem traz CD, LP, fax, engov e whisky JB?",
        "Jane quer LP, fax, CD, giz, TV e bom whisky.",
        "Bancos fúteis pagavam-lhe queijo e whisky xadrez.",
        "Blitz prende ex-vesgo com cheque fajuto.",
        "Jovem craque belga prediz falhas no xote.",
        "Um pequeno jabuti xereta viu dez cegonhas felizes.",
        "Gazeta publica hoje breve nota de faxina na quermesse.",
        "Juiz faz com que whisky de malte baixe logo preco de venda.",
        "Zebras caolhas de Java querem mandar fax para moca gigante de New York.",
        "3",
        "o rato roeu a roupa de margarida",
        "O Rato Roeu A Roupa de Margarida",
        "OlA TuDo CeRtO" };

    for( i = 0; i < sizeof(entrada) / sizeof(entrada[0]); i++ )
        printf( "[%s] => [%s]\n", converter( saida, entrada[i] ), entrada[i]);

    return 0;
}

Exit:

[004297057583] => [Quem traz CD, LP, fax, engov e whisky JB?]
[403460478350] => [Jane quer LP, fax, CD, giz, TV e bom whisky.]
[803248594885] => [Bancos fúteis pagavam-lhe queijo e whisky xadrez.]
[818955743344] => [Blitz prende ex-vesgo com cheque fajuto.]
[441422706041] => [Jovem craque belga prediz falhas no xote.]
[625460434901] => [Um pequeno jabuti xereta viu dez cegonhas felizes.]
[005490501182] => [Gazeta publica hoje breve nota de faxina na quermesse.]
[408550524260] => [Juiz faz com que whisky de malte baixe logo preco de venda.]
[341708204170] => [Zebras caolhas de Java querem mandar fax para moca gigante de New York.]
[] => [3]
[470947440074] => [o rato roeu a roupa de margarida]
[290949440694] => [O Rato Roeu A Roupa de Margarida]
[216405474992] => [OlA TuDo CeRtO]
  • Well I saw that the result, they gave certain, only that this solution is a little complicated for me to understand, you could edit, the part that can put several test cases as the question asks? Thank you in advance

  • Thanks for the cool tips, is who is starting is wrong you learn, unfortunately the solution has not passed, now I’m not worried about the solution, I know it has nothing to do with the question post, but you could recommend me some book for those who are starting to program ?

Browser other questions tagged

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