Doubt with foreach C#

Asked

Viewed 63 times

-2

please. see the code at this link: https://pastebin.com/raw/rDCZQhtf and tell me why the sentence doesn’t come out with the jumbled lyrics; if possible, give me an idea of how to do the same thing with Menas line


string frase,frase0;
    Console.WriteLine("Digite a frase: ");
    frase0 = Console.ReadLine();
    frase = frase0; 
    foreach(var a in frase0) {
            if(a == 'a'){
                frase.Replace(a, 'g');
            }else if(a == 'b'){
                frase.Replace(a, 'h');
            }else if(a == 'c'){
                frase.Replace(a, 'e');
            }else if(a == 'd'){
                frase.Replace(a, 'j');
            }else if(a == 'e'){
                frase.Replace(a, 'k');
            }else if(a == 'f'){
                frase.Replace(a, 'l');
            }else if(a == 'g'){
                frase.Replace(a, 'm');
            }else if(a == 'h'){
                frase.Replace(a, 'n');
            }else if(a == 'i'){
                frase.Replace(a, 'o');
            }else if(a == 'j'){
                frase.Replace(a, 'p');
            }else if(a == 'k'){
                frase.Replace(a, 'q');
            }else if(a == 'l'){
                frase.Replace(a, 'r');
            }else if(a == 'm'){
                frase.Replace(a, 's');
            }else if(a == 'n'){
                frase.Replace(a, 't');
            }else if(a == 'o'){
                frase.Replace(a, 'u');
            }else if(a == 'p'){
                frase.Replace(a, 'v');
            }else if(a == 'q'){
                frase.Replace(a, 'w');
            }else if(a == 'r'){
                frase.Replace(a, 'x');
            }else if(a == 's'){
                frase.Replace(a, 'y');
            }else if(a == 't'){
                frase.Replace(a, 'z');
            }else if(a == 'u'){
                frase.Replace(a, 'a');
            }else if(a == 'v'){
                frase.Replace(a, 'b');
            }else if(a == 'w'){
                frase.Replace(a, 'c');
            }else if(a == 'x'){
                frase.Replace(a, 'd');
            }else if(a == 'y'){
                frase.Replace(a, 'e');
            }else if(a == 'z'){
                frase.Replace(a, 'f');
            }
        }
        Console.WriteLine(frase);

//that’s just the part that’s not working

  • put the code here in the question please

  • Use the link [Edit] to add information to the question. The space below you used is for answers only. Take the opportunity to explain in more detail what the problem is, what the code should do (what the result should be) versus what it is doing, etc. In other words, do what we call [mcve]

  • First you need to define a criterion for shuffling, it should be random, it should be just a simple switch as it was and it will always be so (which technically is not shuffling). If doing what you put in the code is not scrambling and it’s not just too many lines, it’s extremely inefficient.

1 answer

1

I think the code below solves your problem with fewer lines, provided that the association between the letters is always the one you indicated in your question:

string frase0 = string.Empty;
char[] frase = null;
string abc = "abcdefghijklmnopqrstuvwxyz";

Console.WriteLine("Digite a frase: ");

frase0 = Console.ReadLine();
frase = frase0.ToCharArray();

for (int i = 0; i < frase0.Length; i++)
{
    int index = abc.IndexOf(frase0[i]);
    char newChar = '\0';

    if (frase0[i] == ' ')
        continue;

    if ((index + 6) >= abc.Length)
        newChar = abc[0 + (6 - (abc.Length - index))];
    else newChar = abc[index + 6];

    frase[i] = newChar;
}

Console.WriteLine(new string(frase));

Browser other questions tagged

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