Error in data printing

Asked

Viewed 44 times

1

input example:

rmtpuzcafhnyxdesivlkbwgjqo
2
roahp
uhchch

exit :

veras
batata

Code:

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

int main()
{
    char palavra[ 1000 ];
    char alfabeto[ 26 ];
    char codificacao[ 1000 ];
    int N, i, tam, j, indice;

    scanf( "%s", alfabeto );

    scanf( "%d", &N );

    for( i = 0; i < N; i++ ) 
    {
        scanf( "%s", palavra );
    }

    tam = strlen( palavra );

    for( j = 0; j < tam; j++ )
    {
        palavra[ j ] = toupper( palavra[ j ] );
        indice = palavra[ j ] - 65;
        codificacao[ j ] = alfabeto[ indice ];
    }
    printf( "%s\n", codificacao );

    return 0;
}
  • your print must be inside the for

  • When I put it inside the is, it prints character by character of the last output, in case, b,ba,bat,bata,batat,potato,...

  • do not know how it is done in C, however, you can inside your for, concatenate the strings and insert in a variable, so at the end, outside of the for, printe the result

1 answer

1

You read N times the string word and overwrite all readings, so it will only treat the last one. To treat word for word do:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
/*exemplo de entrada

rmtpuzcafhnyxdesivlkbwgjqo
2
roahp
uhchch

saida
veras
batata
*/


int main(){
    char palavra[1000];
    char alfabeto[26];
    char codificacao[1000];
    int N, i, tam, j, indice;
    scanf("%s", alfabeto);
    scanf("%d", &N);
    for(i = 0; i < N; i++) {
        scanf("%s", palavra);
        tam = strlen(palavra);
        for(j = 0; j < tam; j++){
            palavra[j] = toupper(palavra[j]);
            indice = palavra[j] - 65;
            codificacao[j] = alfabeto[indice];
        }
        printf("%s\n", codificacao);
    }
    return 0;
}
  • Mt obg mano, but group each input it already gives the exit, as I do to put tds entrances and print the exits only dps?

  • uses printf out of the for

  • In this case you will need to make dynamic allocation to allocate memory to store the N input words along with the N output encodings, and only print all encodings at the end.

  • I can’t use dynamic allocation

  • At each reading, treat the read word, record the output in a file and at the end of the treatment of all words print the entire file.

  • Depending on the limits that the variable N can assume you can define a rather large string and put each encoding in this string, followed by a ' n' character and print that string at the end. But this solution will not be general.

  • In this case the ideal is, at each input, you check if there is space in the string to store the encoded output.

  • How do I do it??

  • Assuming you are going to concatenate the encodings into a string called TAM_MAX dimension output, do the following test: if (TAM_MAX - strlen(output) > strlen(encoding)) { strcat(output, encoding); strcat(output, "n"); } Else { print("Error. Does not support string. n"); Exit(1); }

Show 4 more comments

Browser other questions tagged

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