Changing letters in C

Asked

Viewed 148 times

2

I have a question, I’m making an algorithm that takes the phrase of the user that has no meaning, as if it were a code, to solve has a key that is the letters that most comes out of the Portuguese alphabet, To solve I have to associate each letter of the text with the letter that most used in English, EX c =a, z=o and so on, I made the code only that it does not change is always the same letter that the user typed. could someone 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);  
}

1 answer

0

I found some errors in your like, for example, if (frase[i]=='c') frase[i]='a'; and if I’m not mistaken you wanted so if (texto[i]=='c') frase[i]='a';

I changed a few more things in your code and it’s 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);  
}
  • thank you very much, I don’t know why when I type C it appears D, it would have to be A, but thank you very much.

  • Another question that has arisen, how do you make it so that when two letters repeat in the sequence, assign a weight to them?

  • Sorry I didn’t understand it very well, but like, AC = S kind of?

  • 1

    I who did not explain well, according to the key that are the letters of the alphabet that most repeat, EX a, and, each of them appear a number of times, as the A that is 35 times the O 16, but so it is easy to assign a value in the text that I want to change, but let’s assume that in the text there is a letter that appears 11, and in the alphabet that letter is i,p and q for example, how do I make the program choose the right letter.

  • Let me get this straight. So if the user writes AAAAABB, in this case we have 5 A then in the alphabet the letter that is at position 5 is E, then all those A will be E, getting EEEEEBB. Well that’s what I understood.

  • I don’t say in the position but the number of letters that repeat, for example, in the Portuguese alphabet the letter that is most used is A, the second is E, let’s say that has a phrase like rtttxxxyyyrrr, the letter A appears 4 times, then it turns on the output of the algorithm, y 3 as it is the second most appears it turns E, so far it’s quiet, only I have a problem, how to make the letters ttt and xxx turn the corresponding letter on the exit, because they appear 3 times. If you can give me a hint, I’ll accept

Show 1 more comment

Browser other questions tagged

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