# Program for Morse Code Translator (Conversion of Morse Code to English Text)

Last Updated : 28 Dec, 2023

Design a Morse Code Translator that converts Morse code into English text. The Morse code consists of dots (.) and dashes (-) representing letters and numbers. Spaces(‘ ‘) separate letters, and forward slash(‘/’) separate words. Implement a program to perform this translation.

#### Examples:

Input: Morse Code: “… — …”
Output: English Text: “SOS”
Explanation: The Morse code “…” represents ‘S’, “—‘ represents ‘O’, and the spaces separate the letters. Therefore, the translation is “SOS.”

Input: Morse Code: “– — .-. … . / -.-. — -.. . / .. … / ..-. — .-. –. . – – .- -… .-.. .”
Output: English Text: “MORSE CODE IS FORGETTABLE”
Explanation: Each Morse code sequence represents the corresponding letter or word. The spaces between Morse code sequences indicate separation between letters and ‘/’ represents spaces between words.

Approach: To solve the problem, follow the below idea:

The problem can be solved by using a map or dictionary to store the letters and digits corresponding to morse code. Split the morse code into words (separated by three spaces) and then into individual letters (separated by single space). After separating the letters look up for each letter in the dictionary to find the corresponding English letter.

Step-by-step algorithm:

• Initialize Morse Code Dictionary:
• Create a dictionary mapping Morse code sequences to English letters and numbers.
• Split Morse Code:
• Split the input Morse code into words and then into individual letters.
• Translate Morse Code:
• For each Morse code letter, look up the corresponding English letter in the dictionary.
• Build English Text:
• Concatenate the translated letters to build the English text.

Illustration of algorithm:

Let’s take the Morse code input `"... --- ..."` as an example:

1. Split into words: `["...", "---", "..."]`
2. Split into letters: `[['.'], ['---'], ['...']]`
3. Translate Morse code: `[['S'], ['O'], ['S']]`
4. Build English text: `'SOS'`

Below is the implementation of the above approach:

## C++

 `#include ` `#include `   `void` `morseToText(std::string morseCode)` `{` `    ``std::map morseCodeDict` `        ``= { { ``".-"``, ``'A'` `},    { ``"-..."``, ``'B'` `},` `            ``{ ``"-.-."``, ``'C'` `},  { ``"-.."``, ``'D'` `},` `            ``{ ``"."``, ``'E'` `},     { ``"..-."``, ``'F'` `},` `            ``{ ``"--."``, ``'G'` `},   { ``"...."``, ``'H'` `},` `            ``{ ``".."``, ``'I'` `},    { ``".---"``, ``'J'` `},` `            ``{ ``"-.-"``, ``'K'` `},   { ``".-.."``, ``'L'` `},` `            ``{ ``"--"``, ``'M'` `},    { ``"-."``, ``'N'` `},` `            ``{ ``"---"``, ``'O'` `},   { ``".--."``, ``'P'` `},` `            ``{ ``"--.-"``, ``'Q'` `},  { ``".-."``, ``'R'` `},` `            ``{ ``"..."``, ``'S'` `},   { ``"-"``, ``'T'` `},` `            ``{ ``"..-"``, ``'U'` `},   { ``"...-"``, ``'V'` `},` `            ``{ ``".--"``, ``'W'` `},   { ``"-..-"``, ``'X'` `},` `            ``{ ``"-.--"``, ``'Y'` `},  { ``"--.."``, ``'Z'` `},` `            ``{ ``"-----"``, ``'0'` `}, { ``".----"``, ``'1'` `},` `            ``{ ``"..---"``, ``'2'` `}, { ``"...--"``, ``'3'` `},` `            ``{ ``"....-"``, ``'4'` `}, { ``"....."``, ``'5'` `},` `            ``{ ``"-...."``, ``'6'` `}, { ``"--..."``, ``'7'` `},` `            ``{ ``"---.."``, ``'8'` `}, { ``"----."``, ``'9'` `},` `            ``{ ``"/"``, ``' '` `} };`   `    ``size_t` `pos = 0;`   `    ``while` `((pos = morseCode.find(``' '``))` `           ``!= std::string::npos) {` `        ``std::string token = morseCode.substr(0, pos);` `        ``if` `(morseCodeDict.find(token)` `            ``!= morseCodeDict.end()) {` `            ``std::cout << morseCodeDict[token];` `        ``}` `        ``else` `{` `            ``std::cout << ``" "``;` `        ``}` `        ``morseCode.erase(0, pos + 1);` `    ``}` `    ``std::cout << morseCodeDict[morseCode] << std::endl;` `}`   `int` `main()` `{` `    ``std::string morseCode` `        ``= ``"-- --- .-. ... . / -.-. --- -.. . / .. ... / ..-. --- .-. --. . - - .- -... .-.. ."``;` `    ``std::cout << ``"Morse Code: "` `<< morseCode << std::endl;` `    ``std::cout << ``"English Text: "``;` `    ``morseToText(morseCode);` `    ``return` `0;` `}`

## C

 `#include ` `#include `   `void` `morseToText(``char` `morseCode[])` `{` `    ``char``* morseCodeDict[]` `        ``= { ``".-"``,   ``"-..."``, ``"-.-."``, ``"-.."``,  ``"."``,    ``"..-."``,` `            ``"--."``,  ``"...."``, ``".."``,   ``".---"``, ``"-.-"``,  ``".-.."``,` `            ``"--"``,   ``"-."``,   ``"---"``,  ``".--."``, ``"--.-"``, ``".-."``,` `            ``"..."``,  ``"-"``,    ``"..-"``,  ``"...-"``, ``".--"``,  ``"-..-"``,` `            ``"-.--"``, ``"--.."` `};`   `    ``char``* token = ``strtok``(morseCode, ``" "``);` `    ``while` `(token != NULL) {` `        ``for` `(``int` `i = 0; i < 26; i++) {` `            ``if` `(``strcmp``(token, morseCodeDict[i]) == 0) {` `                ``printf``(``"%c"``, ``'A'` `+ i);` `                ``break``;` `            ``}` `        ``}` `        ``if` `(``strcmp``(token, ``"/"``) == 0) {` `            ``printf``(``" "``);` `        ``}`   `        ``token = ``strtok``(NULL, ``" "``);` `    ``}` `}`   `int` `main()` `{` `    ``char` `morseCode[]` `        ``= ``"-- --- .-. ... . / -.-. --- -.. . / .. ... / "` `          ``"..-. --- .-. --. . - - .- -... .-.. ."``;` `    ``printf``(``"Morse Code: %s\n"``, morseCode);` `    ``printf``(``"English Text: "``);` `    ``morseToText(morseCode);` `    ``printf``(``"\n"``);` `    ``return` `0;` `}`

## Java

 `import` `java.util.HashMap;`   `public` `class` `MorseCodeTranslator {`   `    ``public` `static` `String morseToText(String morseCode)` `    ``{` `        ``HashMap morseCodeDict` `            ``= ``new` `HashMap<>();` `        ``morseCodeDict.put(``".-"``, ``"A"``);` `        ``morseCodeDict.put(``"-..."``, ``"B"``);` `        ``morseCodeDict.put(``"-.-."``, ``"C"``);` `        ``morseCodeDict.put(``"-.."``, ``"D"``);` `        ``morseCodeDict.put(``"."``, ``"E"``);` `        ``morseCodeDict.put(``"..-."``, ``"F"``);` `        ``morseCodeDict.put(``"--."``, ``"G"``);` `        ``morseCodeDict.put(``"...."``, ``"H"``);` `        ``morseCodeDict.put(``".."``, ``"I"``);` `        ``morseCodeDict.put(``".---"``, ``"J"``);` `        ``morseCodeDict.put(``"-.-"``, ``"K"``);` `        ``morseCodeDict.put(``".-.."``, ``"L"``);` `        ``morseCodeDict.put(``"--"``, ``"M"``);` `        ``morseCodeDict.put(``"-."``, ``"N"``);` `        ``morseCodeDict.put(``"---"``, ``"O"``);` `        ``morseCodeDict.put(``".--."``, ``"P"``);` `        ``morseCodeDict.put(``"--.-"``, ``"Q"``);` `        ``morseCodeDict.put(``".-."``, ``"R"``);` `        ``morseCodeDict.put(``"..."``, ``"S"``);` `        ``morseCodeDict.put(``"-"``, ``"T"``);` `        ``morseCodeDict.put(``"..-"``, ``"U"``);` `        ``morseCodeDict.put(``"...-"``, ``"V"``);` `        ``morseCodeDict.put(``".--"``, ``"W"``);` `        ``morseCodeDict.put(``"-..-"``, ``"X"``);` `        ``morseCodeDict.put(``"-.--"``, ``"Y"``);` `        ``morseCodeDict.put(``"--.."``, ``"Z"``);` `        ``morseCodeDict.put(``"-----"``, ``"0"``);` `        ``morseCodeDict.put(``".----"``, ``"1"``);` `        ``morseCodeDict.put(``"..---"``, ``"2"``);` `        ``morseCodeDict.put(``"...--"``, ``"3"``);` `        ``morseCodeDict.put(``"....-"``, ``"4"``);` `        ``morseCodeDict.put(``"....."``, ``"5"``);` `        ``morseCodeDict.put(``"-...."``, ``"6"``);` `        ``morseCodeDict.put(``"--..."``, ``"7"``);` `        ``morseCodeDict.put(``"---.."``, ``"8"``);` `        ``morseCodeDict.put(``"----."``, ``"9"``);` `        ``morseCodeDict.put(``"/"``, ``" "``);`   `        ``StringBuilder translatedText = ``new` `StringBuilder();` `        ``String[] words = morseCode.split(``"/"``);`   `        ``for` `(String word : words) {` `            ``String[] letters = word.split(``" "``);` `            ``for` `(String letter : letters) {` `                ``translatedText.append(` `                    ``morseCodeDict.getOrDefault(letter, ``""``));` `            ``}` `            ``translatedText.append(` `                ``" "``); ``// Add space between words` `        ``}`   `        ``return` `translatedText.toString()` `            ``.trim(); ``// Remove trailing space` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``String morseCodeInput` `            ``= ``"-- --- .-. ... . / -.-. --- -.. . / .. ... / ..-. --- .-. --. . - - .- -... .-.. ."``;` `        ``System.out.println(``"Morse Code: "` `+ morseCodeInput);` `        ``System.out.println(``"English Text: "` `                           ``+ morseToText(morseCodeInput));` `    ``}` `}`

## Python3

 `def` `morse_to_text(morse_code):` `    ``morse_code_dict ``=` `{``".-"``: ``'A'``, ``"-..."``: ``'B'``, ``"-.-."``: ``'C'``, ``"-.."``: ``'D'``, ``"."``: ``'E'``, ``"..-."``: ``'F'``,` `                       ``"--."``: ``'G'``, ``"...."``: ``'H'``, ``".."``: ``'I'``, ``".---"``: ``'J'``, ``"-.-"``: ``'K'``, ``".-.."``: ``'L'``,` `                       ``"--"``: ``'M'``, ``"-."``: ``'N'``, ``"---"``: ``'O'``, ``".--."``: ``'P'``, ``"--.-"``: ``'Q'``, ``".-."``: ``'R'``,` `                       ``"..."``: ``'S'``, ``"-"``: ``'T'``, ``"..-"``: ``'U'``, ``"...-"``: ``'V'``, ``".--"``: ``'W'``, ``"-..-"``: ``'X'``,` `                       ``"-.--"``: ``'Y'``, ``"--.."``: ``'Z'``,` `                       ``"-----"``: ``'0'``, ``".----"``: ``'1'``, ``"..---"``: ``'2'``, ``"...--"``: ``'3'``, ``"....-"``: ``'4'``,` `                       ``"....."``: ``'5'``, ``"-...."``: ``'6'``, ``"--..."``: ``'7'``, ``"---.."``: ``'8'``, ``"----."``: ``'9'``,` `                       ``"/"``: ``' '``}`   `    ``words ``=` `morse_code.split(``'/'``)  ``# Words are separated by three spaces` `    ``translated_text ``=` `''`   `    ``for` `word ``in` `words:` `        ``letters ``=` `word.split(``' '``)` `        ``for` `letter ``in` `letters:` `            ``# Default to empty string if not found` `            ``translated_text ``+``=` `morse_code_dict.get(letter, '')` `        ``translated_text ``+``=` `' '`  `# Add space between words`   `    ``return` `translated_text.strip()  ``# Remove trailing space`     `# Example usage:` `morse_code_input ``=` `"-- --- .-. ... . / -.-. --- -.. . / .. ... / ..-. --- .-. --. . - - .- -... .-.. ."` `english_text_output ``=` `morse_to_text(morse_code_input)` `print``(``"Morse Code:"``, morse_code_input)` `print``(``"English Text:"``, english_text_output)`

## Javascript

 `function` `morseToText(morseCode) {` `    ``const morseCodeDict = {` `        ``".-"``: ``'A'``, ``"-..."``: ``'B'``, ``"-.-."``: ``'C'``, ``"-.."``: ``'D'``, ``"."``: ``'E'``, ``"..-."``: ``'F'``,` `        ``"--."``: ``'G'``, ``"...."``: ``'H'``, ``".."``: ``'I'``, ``".---"``: ``'J'``, ``"-.-"``: ``'K'``, ``".-.."``: ``'L'``,` `        ``"--"``: ``'M'``, ``"-."``: ``'N'``, ``"---"``: ``'O'``, ``".--."``: ``'P'``, ``"--.-"``: ``'Q'``, ``".-."``: ``'R'``,` `        ``"..."``: ``'S'``, ``"-"``: ``'T'``, ``"..-"``: ``'U'``, ``"...-"``: ``'V'``, ``".--"``: ``'W'``, ``"-..-"``: ``'X'``,` `        ``"-.--"``: ``'Y'``, ``"--.."``: ``'Z'``,` `        ``"-----"``: ``'0'``, ``".----"``: ``'1'``, ``"..---"``: ``'2'``, ``"...--"``: ``'3'``, ``"....-"``: ``'4'``,` `        ``"....."``: ``'5'``, ``"-...."``: ``'6'``, ``"--..."``: ``'7'``, ``"---.."``: ``'8'``, ``"----."``: ``'9'``,` `        ``"/"``: ``' '` `    ``};`   `    ``const words = morseCode.split(``'/'``);  ``// Words are separated by three spaces` `    ``let translatedText = ``''``;`   `    ``for` `(const word of words) {` `        ``const letters = word.split(``' '``);` `        ``for` `(const letter of letters) {` `            ``translatedText += morseCodeDict[letter] || ``''``;  ``// Default to empty string if not found` `        ``}` `        ``translatedText += ``' '``;  ``// Add space between words` `    ``}`   `    ``return` `translatedText.trim();  ``// Remove trailing space` `}`   `// Example usage:` `const morseCodeInput = ``"... --- ..."``;` `const englishTextOutput = morseToText(morseCodeInput);` `console.log(``"Morse Code:"``, morseCodeInput);` `console.log(``"English Text:"``, englishTextOutput);`

Output

```Morse Code: -- --- .-. ... . / -.-. --- -.. . / .. ... / ..-. --- .-. --. . - - .- -... .-.. .
English Text: MORSE CODE IS FORGETTABLE
```

Time Complexity: O(N), where N is the length of the Morse code input string. This is because we iterate through the Morse code input string once, and each operation inside the loop is constant time.
Auxiliary Space: O(1), as we use constant space for the dictionary.