Changing letters in C [duplicate]

2

I have a question, I'm making an algorithm that takes the user's phrase that does not have sense, as if it were a code, to solve it has a key that is the letters that come out most of the Portuguese alphabet, to solve I have to associate each letter of the text with the most used letter in Portuguese, EX c = a, z = o and so on, I made the code only it does not change is always the same letter that the user typed. could anyone give me some tips. '# include

#include <string.h>
main()
{
    char chave[20],texto[20],frase[20];
    int size,i;
    printf("Digite o texto para ser alterado: ");
    scanf("%s",chave);
    size=strlen(chave);
    strcpy(texto,chave);  
    for(i=0;i<size;i++)
    {
        if (frase[i]=='c')
            frase[i]='a';
        else if (texto[i]=='b')
            frase[i]='s';
        else if (texto[i]=='d')
            frase[i]='e';
        else if (texto[i]=='e')
            frase[i]='d';
    } 
    printf("Texto original:\n%s\n Novo texto:\n%s\n",chave,texto);  
}
    
asked by anonymous 21.04.2018 / 18:46

1 answer

0

I found some errors in yours, such as if (frase[i]=='c') frase[i]='a'; and if I'm not mistaken you wanted to if (texto[i]=='c') frase[i]='a';

I changed some more things in your code and it's already working.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main()
{
    char chave[20], texto[20], frase[20];
    int size,i;

    printf("Digite o texto para ser alterado: ");
    scanf("%s",chave);

    size=strlen(chave);
    strcpy(texto, chave);  

    for(i=0;i<size;i++)
    {
        if (texto[i]=='c')
            frase[i]='a';
        else if (texto[i]=='b')
            frase[i]='s';
        else if (texto[i]=='d')
            frase[i]='e';
        else if (texto[i]=='e')
            frase[i]='d';
        else frase[i]=texto[i];
    } 
    printf("Texto original:\n %s\nNovo texto:\n %s\n", chave, frase);  
}
    
21.04.2018 / 19:10