Introduction
Morse code is a method of encoding text characters as sequences of two different signal durations, called
dots and dashes. It was widely used in telecommunication in the past, particularly in radio communications.
In this program, we will demonstrate how to translate text to Morse code and vice versa using the C++
programming language. This provides a simple way to understand how Morse code can be implemented in software.
Objective
The objective of this program is to:
- Translate text (English characters) into Morse code.
- Translate Morse code back to text.
- Provide a user-friendly interface to input text or Morse code for translation.
C++ Program Code
#include #include #include #include using namespace std; // Function to initialize the morse code mapping void initializeMorseCode(unordered_map<char, string> &morseCodeMap) { morseCodeMap['A'] = ".-"; morseCodeMap['B'] = "-..."; morseCodeMap['C'] = "-.-."; morseCodeMap['D'] = "-.."; morseCodeMap['E'] = "."; morseCodeMap['F'] = "..-."; morseCodeMap['G'] = "--."; morseCodeMap['H'] = "...."; morseCodeMap['I'] = ".."; morseCodeMap['J'] = ".---"; morseCodeMap['K'] = "-.-"; morseCodeMap['L'] = ".-.."; morseCodeMap['M'] = "--"; morseCodeMap['N'] = "-."; morseCodeMap['O'] = "---"; morseCodeMap['P'] = ".--."; morseCodeMap['Q'] = "--.-"; morseCodeMap['R'] = ".-."; morseCodeMap['S'] = "..."; morseCodeMap['T'] = "-"; morseCodeMap['U'] = "..-"; morseCodeMap['V'] = "...-"; morseCodeMap['W'] = ".--"; morseCodeMap['X'] = "-..-"; morseCodeMap['Y'] = "-.--"; morseCodeMap['Z'] = "--.."; morseCodeMap['0'] = "-----"; morseCodeMap['1'] = ".----"; morseCodeMap['2'] = "..---"; morseCodeMap['3'] = "...--"; morseCodeMap['4'] = "....-"; morseCodeMap['5'] = "....."; morseCodeMap['6'] = "-...."; morseCodeMap['7'] = "--..."; morseCodeMap['8'] = "---.."; morseCodeMap['9'] = "----."; morseCodeMap[' '] = "/"; // Space between words } // Function to translate text to Morse code string textToMorse(string text, unordered_map<char, string> &morseCodeMap) { stringstream morseStream; for (char &ch : text) { ch = toupper(ch); // Convert to uppercase if (morseCodeMap.find(ch) != morseCodeMap.end()) { morseStream << morseCodeMap[ch] << " "; } } return morseStream.str(); } // Function to translate Morse code to text string morseToText(string morse, unordered_map<string, char> &reverseMorseCodeMap) { stringstream textStream; string morseLetter; stringstream morseStream(morse); while (morseStream >> morseLetter) { if (reverseMorseCodeMap.find(morseLetter) != reverseMorseCodeMap.end()) { textStream << reverseMorseCodeMap[morseLetter]; } } return textStream.str(); } int main() { unordered_map<char, string> morseCodeMap; unordered_map<string, char> reverseMorseCodeMap; // Initialize the Morse Code map initializeMorseCode(morseCodeMap); // Reverse the morseCodeMap to facilitate Morse to text translation for (const auto &pair : morseCodeMap) { reverseMorseCodeMap[pair.second] = pair.first; } int choice; cout << "Morse Code Translator\n"; cout << "1. Text to Morse Code\n"; cout << "2. Morse Code to Text\n"; cout << "Enter your choice: "; cin >> choice; cin.ignore(); // to ignore the newline character if (choice == 1) { string text; cout << "Enter text: "; getline(cin, text); string morse = textToMorse(text, morseCodeMap); cout << "Morse Code: " << morse << endl; } else if (choice == 2) { string morse; cout << "Enter Morse Code (separate letters with spaces and words with '/'): "; getline(cin, morse); string text = morseToText(morse, reverseMorseCodeMap); cout << "Text: " << text << endl; } else { cout << "Invalid choice!" << endl; } return 0; }
Explanation of the Program Structure
The program begins by defining a unordered_map
to store the mappings for Morse code, with characters as keys and their Morse equivalents as values. The program also provides reverse mappings from Morse code back to text for efficient decoding.
The initializeMorseCode
function initializes the Morse code dictionary, which is later used for encoding and decoding. The textToMorse
function converts plain text to Morse code, while morseToText
converts Morse code back to text.
The user is prompted to choose whether they want to encode text into Morse code or decode Morse code into text. Based on the user’s choice, the appropriate function is called to perform the translation.
How to Run the Program
To run this C++ program, follow these steps:
- Copy the code provided above into a text file and save it with a
.cpp
extension (e.g.,morse_code_translator.cpp
). - Open your terminal or command prompt.
- Navigate to the directory where the C++ file is saved.
- Compile the program using a C++ compiler by running the following command:
g++ morse_code_translator.cpp -o morse_code_translator
. - Run the compiled program with the command:
./morse_code_translator
(ormorse_code_translator.exe
on Windows). - Follow the on-screen instructions to translate text to Morse code or Morse code back to text.