//Sam Partovi CS1A Ch. 10, P.591, #17
/*******************************************************************************
* CONVERT STRING TO MORSE CODE
* ____________________________________________________________
* This program accepts a string from the user, converts it into Morse code,
* and displays the result.
* ____________________________________________________________
*INPUT
* userString: The string entered by the user.
*
*OUTPUT
* MorseCode: The converted string in Morse code.
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
//Function prototypes
void GetMorseCode(char c, string& morseCode);
void ConvertToMorseCode(const string& input);
int main() {
string userString;
// Prompt the user to enter a string
cout << "Enter a string to convert to Morse code: ";
getline(cin, userString);
// Convert and display the Morse code
ConvertToMorseCode(userString);
return 0;
}
/*******************************************************************************
*Function definition for GetMorseCode
* This function finds the corresponding morse code for individual characters.
*******************************************************************************/
void GetMorseCode(char c, string& morseCode) {
//Arrays for characters and their corresponding Morse codes
const char characters[] = {
'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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '
};
const string morseCodes[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----.", "-----", " "
};
const int numCharacters = sizeof(characters) / sizeof(characters[0]);
char upperChar = toupper(c); //Convert to uppercase for consistency
//Search for the character in the characters array
for (int i = 0; i < numCharacters; i++) {
if (characters[i] == upperChar) {
morseCode = morseCodes[i]; // Get the Morse code
break;
}
}
}
/*******************************************************************************
*Function definition for ConvertToMorseCode
* This function converts given input to its morse code equivalent.
*******************************************************************************/
void ConvertToMorseCode(const string& input) {
cout << "\nMorse Code: ";
for (char c : input) {
string morseCode;
GetMorseCode(c, morseCode); //Get Morse code for the character
cout << morseCode << " "; //Output the Morse code
}
cout << endl;
}
Ly9TYW0gUGFydG92aSAgICAgICAgICAgICAgICAgICAgICAgICAgQ1MxQSAgICAgICAgICAgICAgICAgICBDaC4gMTAsIFAuNTkxLCAjMTcKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKKiBDT05WRVJUIFNUUklORyBUTyBNT1JTRSBDT0RFCiogX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiogVGhpcyBwcm9ncmFtIGFjY2VwdHMgYSBzdHJpbmcgZnJvbSB0aGUgdXNlciwgY29udmVydHMgaXQgaW50byBNb3JzZSBjb2RlLCAKKiBhbmQgZGlzcGxheXMgdGhlIHJlc3VsdC4KKiBfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KKklOUFVUCiogIHVzZXJTdHJpbmc6IFRoZSBzdHJpbmcgZW50ZXJlZCBieSB0aGUgdXNlci4KKgoqT1VUUFVUCiogICBNb3JzZUNvZGU6IFRoZSBjb252ZXJ0ZWQgc3RyaW5nIGluIE1vcnNlIGNvZGUuCioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vRnVuY3Rpb24gcHJvdG90eXBlcwp2b2lkIEdldE1vcnNlQ29kZShjaGFyIGMsIHN0cmluZyYgbW9yc2VDb2RlKTsKdm9pZCBDb252ZXJ0VG9Nb3JzZUNvZGUoY29uc3Qgc3RyaW5nJiBpbnB1dCk7CgppbnQgbWFpbigpIHsKICAgIHN0cmluZyB1c2VyU3RyaW5nOwoKICAgIC8vIFByb21wdCB0aGUgdXNlciB0byBlbnRlciBhIHN0cmluZwogICAgY291dCA8PCAiRW50ZXIgYSBzdHJpbmcgdG8gY29udmVydCB0byBNb3JzZSBjb2RlOiAiOwogICAgZ2V0bGluZShjaW4sIHVzZXJTdHJpbmcpOwoKICAgIC8vIENvbnZlcnQgYW5kIGRpc3BsYXkgdGhlIE1vcnNlIGNvZGUKICAgIENvbnZlcnRUb01vcnNlQ29kZSh1c2VyU3RyaW5nKTsKCiAgICByZXR1cm4gMDsKfQoKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKKkZ1bmN0aW9uIGRlZmluaXRpb24gZm9yIEdldE1vcnNlQ29kZQoqIFRoaXMgZnVuY3Rpb24gZmluZHMgdGhlIGNvcnJlc3BvbmRpbmcgbW9yc2UgY29kZSBmb3IgaW5kaXZpZHVhbCBjaGFyYWN0ZXJzLgoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqLwp2b2lkIEdldE1vcnNlQ29kZShjaGFyIGMsIHN0cmluZyYgbW9yc2VDb2RlKSB7CiAgICAvL0FycmF5cyBmb3IgY2hhcmFjdGVycyBhbmQgdGhlaXIgY29ycmVzcG9uZGluZyBNb3JzZSBjb2RlcwogICAgY29uc3QgY2hhciBjaGFyYWN0ZXJzW10gPSB7CiAgICAgICAgJ0EnLCAnQicsICdDJywgJ0QnLCAnRScsICdGJywgJ0cnLCAnSCcsICdJJywgJ0onLCAnSycsICdMJywgCiAgICAgICAgJ00nLCAnTicsICdPJywgJ1AnLCAnUScsICdSJywgJ1MnLCAnVCcsICdVJywgJ1YnLCAnVycsICdYJywgCiAgICAgICAgJ1knLCAnWicsICcxJywgJzInLCAnMycsICc0JywgJzUnLCAnNicsICc3JywgJzgnLCAnOScsICcwJywgJyAnCiAgICB9OwogICAgY29uc3Qgc3RyaW5nIG1vcnNlQ29kZXNbXSA9IHsKICA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