textbox to allow multi strings for ciphers codes

Viewed 47

i created my custom cyphers that are strings. when i try to run the code textbox1 is only returning vale of a single code its not appending , eg writing "4 5d" in textbox1 wont give a value but when i change the code from string textCiphered = myCipher.Cipher(textToBeCiphered); to string textCiphered = myCipher.Cipher("4", "5d"); its giving result " take 4 for 5 days"in texbox2. when i type 4 5d in textbox1 nothing is showing in textbox2, i need help in making the textbox1 work

My class code

class MyCipher : ICipherDecipher
{
    private List<Code> alphabet;

    public MyCipher()
    {
        alphabet = new List<Code>();

        alphabet.Add(new Code("4", " take 4"));
        alphabet.Add(new Code("3", " take 3"));
        alphabet.Add(new Code("5d", " for 5 days"));
    }

    public string Cipher(params string[] codes)
    {

        StringBuilder result = new StringBuilder();

        foreach (string code in codes)
        {
            Code element =
                alphabet.Where(x => x.MyCode == code).SingleOrDefault();

            if (element != null)
            {
                result.Append(element.MyDecoded);
            }
        }

        return result.ToString();

    }


    class Code
    {
        public string MyCode;
        public string MyDecoded;



        public Code(string code, string decode)
        {
            MyCode = code;
            MyDecoded = decode;
        }

    }
}

Button code where it needs editing

public partial class Form1 : Form
{
    private ICipherDecipher myCipher;
    public Form1()
    {
        myCipher = new MyCipher();
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string textToBeCiphered = textBox1.Text;
        string textCiphered = myCipher.Cipher(textToBeCiphered);
        textBox2.Text = textCiphered;
    }
}

Nothing is showing after clicking button1 - image

2 Answers

So you're entering one single string, not multiple strings, so it's doing a lookup in your alphabet for "4 5d" and not splitting those into "4", "5d". I suggest changing that argument to just string[] codes not params string[] codes and doing a split on your input so that it gets split by spaces.

In summary:

Change this:

 public string Cipher(params string[] codes)

To:

 public string Cipher(string[] codes)

And change this:

string textCiphered = myCipher.Cipher(textToBeCiphered);

To:

string textCiphered = myCipher.Cipher(textToBeCiphered.Split(' '));

For constructing the dictionary:

alphabet = new Dictionary<string,string>(); 
alphabet.Add("4", " take 4"); 
alphabet.Add("3", " take 3"); 
alphabet.Add("5d", " for 5 days");

Then looking up the code will look like this:

if (alphabet.ContainsKey(code))
   result.Append(alphabet[code]);

Instead of this:

Code element =
        alphabet.Where(x => x.MyCode == code).SingleOrDefault();

if (element != null)
{
    result.Append(element.MyDecoded);
}

Are you splitting up the input string? The textbox.Text gives out the string of "4 5d" in your case, not "4","5d". In this case, it seems that you want to check each 'part' of the string for the cipher. The function String.Split(" ") should get that job done as it splits the string by the spaces into a string array.

Related