Drop links or images here to add them to the editor.

Translating dots and dashes.

★★☆

Morse code is a method used in telecommunication to encode text characters as sequences of two different signal durations, called dots and dashes, or dits and dahs. In the 1890s, Morse code began to be used extensively for early radio communication before it was possible to transmit voice. In the late 19th and early 20th centuries, most high-speed international communication used Morse code on telegraph lines, undersea cables, and radio circuits. Today, Morse code has been replaced by the Global Maritime Distress and Safety System (GMDSS). This program translates Morse code in a file into plain text.

Morse code

Make

Write a program that reads a file containing Morse code with dots represented with a full stop: “.”, dashes represented with hyphens: “-“ and words separated with a space and forward slash: “ / “. The program should output the contents of the file in English.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called read_morse that:

  1. Takes a parameter that is the filename containing the Morse code.
  2. Checks if the file exists. If not, it returns “error: file not found”.
  3. Reads the data in the file.
  4. Returns the message contained in the file in plain text by calling translate for each character.

Complete the main program so that:

  1. The user can input the name of the file.
  2. The read_morse function is called.
  3. The plain text message is output.

Typical inputs and outputs from the program would be:

For a file containing:

... .- -- ..- . .-.. / -- --- .-. ... . / .-- .- ... / .- -. / .- -- . .-. .. -.-. .- -. / .--. .- .. -. - . .-. / .- -. -.. / .. -. ...- . -. - --- .-. / .-- .... --- / .. ... / -... . ... - / .-. . -- . -- -... . .-. . -.. / - --- -.. .- -.-- / ..-. --- .-. / .... .. ... / .. -. ...- . -. - .. --- -. / --- ..-. / - .... . / ... .. -. --. .-.. . / .-- .. .-. . / - . .-.. . --. .-. .- .--. .... / ... -.-- ... - . -- / .- -. -.. / -- --- .-. ... . / -.-. --- -.. .

The output would be:

Enter the filename:
SAMUEL MORSE WAS AN AMERICAN PAINTER AND INVENTOR WHO IS BEST REMEMBERED TODAY FOR HIS INVENTION OF THE SINGLE WIRE TELEGRAPH SYSTEM AND MORSE CODE

The test runner provides the filename on its own line of stdin and ships each messageXX.txt as a workdir file.

🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Morse code program

using System;
using System.IO;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Convert a Morse code character into an English character
---
    static string translate(string morse)
    {
---
        switch (morse)
        {
---
            case ".-":
                return "A";
            case "-...":
                return "B";
            …
            case "--..":
                return "Z";
            default:
                return " ";
---
        }
---
    }
---


    // Read a file containing Morse code and translate it
    static string read_morse(string filename)
    {
---
        // // Check the file exists
        try
        {
---
            string text = "";
            StreamReader file = new StreamReader(filename);
            string morse_code = file.ReadToEnd().Trim();
            string[] character_list = morse_code.Split(new[] { ' ' });
            file.Close();
---
            // Translate each element of the list
            foreach (string morse_character in character_list)
            {
---
                string character = translate(morse_character);
                text += character;
---
            }
---
            return text;
---
        }
---
        catch (FileNotFoundException)
        {
---
            return "error: file not found";
---
        }
---
    }
---


    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the filename:");
        string filename = Console.ReadLine();
        string message = read_morse(filename);
        Console.WriteLine(message);
---
    }
---
}