locked
Chaesar in C# RRS feed

  • Question

  • User242907078 posted

    Hello,
    I made something called Chaesar but I have a problem.

    Dictionary<string, string> English = new Dictionary<string, string>();
                English.Add("a", "§");
                English.Add("b", "╚");
                English.Add("c", "{");
                English.Add("d", "A");
                English.Add("e", "Ä");
                English.Add("f", "█");
                English.Add("g", "Õ");
                English.Add("h", "T");
                English.Add("i", "├");
                English.Add("j", "e");
                English.Add("k", "ƒ");
                English.Add("l", "!");         
                English.Add("m", "á");
                English.Add("n", "┤");          
                English.Add("o", "‗");   
                English.Add("p", "8");    
                English.Add("q", "Ì");
                English.Add("r", "*");
                English.Add("s", "&");
                English.Add("t", "^");
                English.Add("u", "%");
                English.Add("v", "$");
                English.Add("w", "#");
                English.Add("x", "@");
                English.Add("y", "(");
                English.Add("z", "╣");
                English.Add(" ", " ");
                for (int i = 0; i < textBox1.TextLength; i++)
                {
                    string MyChar = textBox1.Text[i].ToString();
                    textBox2.Text += English[MyChar];
                }
            }

    everything works, but if I type in the textbox for example: "A" I get error, so my question is:
    how can I make it with words like: "a", "A", "b", "b".

    (sorry for my bad English)
    and this is the first time I'm here, sorry if Im post in the wrong place

    Monday, December 4, 2017 5:43 PM

All replies

  • User475983607 posted

    string.ToLower();

    Monday, December 4, 2017 6:07 PM
  • User2103319870 posted

    As mgebhard suggested you need to use ToLower to make your comparison case insensitive. however you will still get exception on second iteration onwards as after first replacement text in textbox will be like A§ and this '§' value doesnt exist on dictionary as key.  You might consider using the below Linq code to replace the characters from Dictionary

     var formattedvalue = English.Aggregate(user_email.Text, (current, value) => current.ToLower().Replace(value.Key, value.Value));
    
                user_email.Text = formattedvalue;

    make sure you add the below namespace to your page

    using System.Linq;

    Monday, December 4, 2017 7:58 PM
  • User-707554951 posted

    Hi CodeLikeAl3oSS,

    Working sample as below:

      <asp:TextBox ID="textBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
                <br />
                <asp:TextBox ID="textBox2" runat="server"></asp:TextBox>

    CodeBehind:

     protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                textBox2.Text = "";
                Dictionary<string, string> English = new Dictionary<string, string>();
                English.Add("a", "§");
                English.Add("b", "╚");
                English.Add("c", "{");
                English.Add("d", "A");
                English.Add("e", "Ä");
                English.Add("f", "█");
                English.Add("g", "Õ");
                English.Add("h", "T");
                English.Add("i", "├");
                English.Add("j", "e");
                English.Add("k", "ƒ");
                English.Add("l", "!");
                English.Add("m", "á");
                English.Add("n", "┤");
                English.Add("o", "‗");
                English.Add("p", "8");
                English.Add("q", "Ì");
                English.Add("r", "*");
                English.Add("s", "&");
                English.Add("t", "^");
                English.Add("u", "%");
                English.Add("v", "$");
                English.Add("w", "#");
                English.Add("x", "@");
                English.Add("y", "(");
                English.Add("z", "╣");
                English.Add(" ", " ");
                for (int i = 0; i < textBox1.Text.Length; i++)
                {
                    string MyChar = textBox1.Text[i].ToString().ToLower();
                    textBox2.Text += English[MyChar];
                }
            }

    Output:


    Best regards 

    Cathy

    Tuesday, December 5, 2017 8:59 AM