How should I take an input, check against a dictionary, and if it matches replace with the definition?

What would be the best way to take an input, check against a dictionary, and if it matches replace with the definition?

I'm trying to make a morse code converter, and I've decided to use a dictionary to store the morse translations. I'm struggingling to think of the best way to check the dictionary against an input though.

I could do something like...

If unconvertedInput = morseDic commaMorse

However, that would be terribly inefficient I think. Or is a dictionary not the way to go here? I'm just not sure.

Also here is my incomplete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MorseCodeConverter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> morseDictonary = new Dictionary<string, string>();
            morseDictonary.Add(" ", " ");
            morseDictonary.Add(",", "--..--");
            morseDictonary.Add(".", ".-.-.-");

            string spaceMorse = morseDictonary[" "];
            string commaMorse = morseDictonary[","];
            string periodMorse = morseDictonary["."];
            MessageBox.Show(spaceMorse);
        }
    }
}
Jon Skeet
people
quotationmark

It sounds like you actually want a static variable for the dictionary, as that's not going to change over time. I'd then use a StringBuilder to build up the result, rather than trying to replace text within the string. (Strings are immutable, making that a tricky proposition anyway - and life is often simpler when you keep input and output strictly separated.)

In terms of efficiency, using a StringBuilder is going to be fine - and the dictionary lookups will be very efficient. If you know that you're only dealing with ASCII text, you could have a string[] and index it by ASCII value instead, and that would be even more efficient... but I would stick to the dictionary for now unless you're translating huge amounts of text. (It will still happen in the blink of an eye for any sensible amount of text.)

public class MorseCodeTranslationForm
{
    private static readonly Dictionary<char, string> Translations =
        new Dictionary<char, string>
        {
            { 'A', ".-" },
            { 'B', "-..." },
            // etc 
        };

    // Normal constructor etc

    // Call this from an event handler...
    private static string ConvertToMorse(string text)
    {
        StringBuilder builder = new StringBuilder();
        foreach (char c in text)
        {
            string translated;
            if (Translations.TryGetValue(c, out translation))
            {
                builder.Append(translated).Translate(' ');
            }
            else
            {
                builder.Append(c); // Just leave it as it is                    
            }
        }
        return builder.ToString();
    }
}

(You might also want to separate the UI code from the "business logic" of morse translation, but that's a different matter.)

people

See more on this question at Stackoverflow