//Maxwell Brewer CS1A Chapter 10, p. 591, #17
//
/*******************************************************************************
* TEXT TO MORSE CODE
* _____________________________________________________________________________
* This program takes a user-provided text string and converts it into
* Morse code. Each character is translated to its corresponding Morse code
* representation, based on a predefined set of mappings for letters, digits,
* and common punctuation. Unsupported characters are flagged as invalid.
*
* INPUT:
* inputString : (maximum 100 characters) containing
* : letters, numbers, spaces, and common
* : punctuation marks (e.g., '.', ',', '?').
*
* OUTPUT:
* : The Morse code representation of the input string
*
*******************************************************************************/
#include <iostream>
#include <string>
#include <cctype> // For toupper function
using namespace std;
// Function prototypes
void convertToMorse(const string &input, const string morseCodes[]);
int getMorseIndex(char ch);
// Morse code array for characters and symbols
const string MORSE_CODES[] = {
" ", "--..--", ".-.-.-", "..--..", "-----", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----.", ".-", "-...",
"-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-",
".--", "-..-", "-.--", "--.."
};
int main() {
string inputString;
// Prompt the user for input
cout << "Enter your string\n";
cout << "(Max 100 characters):\n";
getline(cin, inputString); // Read the entire line of input
// Convert the input string to Morse Code
cout << "\nMorse Code Conversion:\n";
convertToMorse(inputString, MORSE_CODES);
return 0;
}
// Function to convert the input string to Morse Code
void convertToMorse(const string &input, const string morseCodes[]) {
for (char ch : input) {
// Convert character to uppercase and find Morse index
int index = getMorseIndex(toupper(ch));
// If the index is valid, print the Morse code
if (index != -1) {
cout << morseCodes[index] << " ";
} else {
cout << "[Invalid] "; // Handle unsupported characters
}
}
cout << endl;
}
// Function to get the Morse code array index for a character
int getMorseIndex(char ch) {
if (ch == ' ') return 0; // Space
if (ch == ',') return 1; // Comma
if (ch == '.') return 2; // Period
if (ch == '?') return 3; // Question mark
if (ch >= '0' && ch <= '9') return ch - '0' + 4; // Numbers 0-9
if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 14; // Letters A-Z
return -1; // Invalid character
}
Ly9NYXh3ZWxsIEJyZXdlciAgICAgICAgICAgICAgICAgQ1MxQSAgICAgICAgICAgICAgICAgICAgQ2hhcHRlciAxMCwgcC4gNTkxLCAjMTcKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogVEVYVCBUTyBNT1JTRSBDT0RFCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIFRoaXMgcHJvZ3JhbSB0YWtlcyBhIHVzZXItcHJvdmlkZWQgdGV4dCBzdHJpbmcgYW5kIGNvbnZlcnRzIGl0IGludG8gCiAqIE1vcnNlIGNvZGUuIEVhY2ggY2hhcmFjdGVyIGlzIHRyYW5zbGF0ZWQgdG8gaXRzIGNvcnJlc3BvbmRpbmcgTW9yc2UgY29kZSAKICogcmVwcmVzZW50YXRpb24sIGJhc2VkIG9uIGEgcHJlZGVmaW5lZCBzZXQgb2YgbWFwcGluZ3MgZm9yIGxldHRlcnMsIGRpZ2l0cywgCiAqIGFuZCBjb21tb24gcHVuY3R1YXRpb24uIFVuc3VwcG9ydGVkIGNoYXJhY3RlcnMgYXJlIGZsYWdnZWQgYXMgaW52YWxpZC4KICogCiAqIElOUFVUOgogKiAgICBpbnB1dFN0cmluZyAgICAgIDogKG1heGltdW0gMTAwIGNoYXJhY3RlcnMpIGNvbnRhaW5pbmcKICogICAgICAgICAgICAgICAgICAgICA6IGxldHRlcnMsIG51bWJlcnMsIHNwYWNlcywgYW5kIGNvbW1vbgogKiAgICAgICAgICAgICAgICAgICAgIDogcHVuY3R1YXRpb24gbWFya3MgKGUuZy4sICcuJywgJywnLCAnPycpLgogKiAKICogT1VUUFVUOgogKiAgICAgICAgICAgICAgICAgICAgIDogVGhlIE1vcnNlIGNvZGUgcmVwcmVzZW50YXRpb24gb2YgdGhlIG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