cplusplus
cplusplus

 

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:

  1. Copy the code provided above into a text file and save it with a .cpp extension (e.g., morse_code_translator.cpp).
  2. Open your terminal or command prompt.
  3. Navigate to the directory where the C++ file is saved.
  4. Compile the program using a C++ compiler by running the following command:
    g++ morse_code_translator.cpp -o morse_code_translator.
  5. Run the compiled program with the command:
    ./morse_code_translator (or morse_code_translator.exe on Windows).
  6. Follow the on-screen instructions to translate text to Morse code or Morse code back to text.

 

© 2024 Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)