Error in data output

Asked

Viewed 55 times

0

I want to print the output exactly as below, but the code is giving the output in each entry.

Entrances

  • rmtpuzcafhnyxdesivlkbwgjqo
  • 3
  • roahp
  • uhchch
  • veras

Exits

  • veras
  • potato
  • wuvrl
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {

    char string[1000];
    char alfabeto[26];
    char misterio[1000];
    int N, i, tam, j, indice,z;

    scanf("%s", alfabeto);
    scanf("%d", &N);

    for(i = 0; i < N; i++) {
        scanf("%s", string);
        tam = strlen(string);

        for(j = 0; j < tam; j++){
            string[j] = toupper(string[j]);
            indice = string[j] - 65;
            misterio[j] = alfabeto[indice];

        }
    printf("%s", misterio);
    }

  return 0;
}
  • Start by separating in the question what is code and what is explanation of the problem itself. Take advantage of detail about what your program is supposed to do and where it’s not doing what it’s supposed to do.

1 answer

2

Hi, from what I understand, you want the words to be printed in the order you put it, so I got your code to do it. It may not be the cleanest solution, but it’s very practical:

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

int main() {

    char string[1000];
    char alfabeto[26];
    char misterio[1000];
    int N, i, tam, j, indice, z, aux=0;   //adicionei a variável aux

    scanf("%s", alfabeto);
    scanf("%d", &N);

    for(i = 0; i < N; i++) {
        scanf("%s", string);
        tam = strlen(string);

        for(j = 0; j < tam; j++) {
            string[j] = toupper(string[j]);
            indice = string[j] - 65;
            misterio[aux] = alfabeto[indice];  //atenção aqui. aux nunca é zerado, sempre incrementado
            aux++;
        }
        misterio[aux] = '\n';  //adiciona o char que pula linha
        aux++;  //passa para a próxima casa do vetor
    }
    misterio[aux] = '\0';   //adiciona char que encerra a string

    printf("%s", misterio);

  return 0;
}

I put the mystery variable to hold all the words you "translated". Basically, the mystery string holds the translated word and then puts a ' n', and goes on like that until the entries are finished. Finally a ' 0' is added to terminate the string and print without wrong symbols.

  • very good solution :)

  • Mt thanks bro!! '-'

Browser other questions tagged

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