BlackWaspTM
Algorithms and Data Structures
.NET 2.0+

Convert Text to Morse Code (2)

Morse code is a standard encoding for letters, digits and some other characters that is ideally suited to radio transmissions and is used by amateur radio operators and pilots. This article describes a simple C# class for converting text to Morse code.

Converting Characters

The conversion of text to Morse code is performed by three methods. At the deepest level, individual characters need to be converted to a series of dots and dashes. This is achieved with the private ConvertCharacter method, which looks up a character in the dictionary and returns the appropriate encoded string. If the character is not present, an empty string is returned. This eliminates punctuation from encoded text without affecting the overall result. Note that this method assumes that the character passed to the 'ch' parameter will be in upper case.

private string ConvertCharacter(char ch)
{
    if (_codes.Keys.Contains(ch))
        return _codes[ch];
    else
        return string.Empty;
}

Converting Words

The second method is used to convert entire words to Morse code, using the ConvertCharacter method for each character. A for-each loop processes each character from the string in turn and adds the result to a StringBuilder. The if statement adds a space before all letters except the first, separating the characters for easy readability.

private string ConvertWord(string word)
{
    StringBuilder sb = new StringBuilder();
    foreach (char ch in word)
    {
        if (sb.Length != 0 && sb[sb.Length - 1] != ' ')
            sb.Append(" ");
        sb.Append(ConvertCharacter(ch));
    }
    return sb.ToString();
}

Converting Text

The final method converts a full string of text into Morse code. Firstly, the input text is converted to upper case and is split into words according to the positioning of space characters. The remainder of the code is similar to the ConvertWord method. All words in turn are passed to ConvertWord and the encoded text is combined into the end result. Each word in the result is separated from the next by a forward slash (/).

public string ConvertText(string sentence)
{
    string[] words = sentence.ToUpper().Split(' ');
    StringBuilder sb = new StringBuilder();
    foreach (string word in words)
    {
        if (sb.Length != 0)
            sb.Append("/");
        sb.Append(ConvertWord(word));
    }
    return sb.ToString();
}

Testing the Converter

To test the converter, add the following code to the Main method of the console application. Run the program and type words and phrases that you wish to convert. When you press Enter, the Morse code is displayed. To exit the program, press Enter without entering any text.

MorseConverter converter = new MorseConverter();

string toConvert;
do
{
    Console.WriteLine("Enter string to convert:");
    toConvert = Console.ReadLine();
    string morse = converter.ConvertText(toConvert);
    Console.WriteLine(morse);
}
while (toConvert.Length != 0);
5 April 2010