Old fashion Morse code uses a series of dots and dashes to stand for letters, di
ID: 3555384 • Letter: O
Question
Old fashion Morse code uses a series of dots and dashes to stand for letters, digits and punctuation marks, this program will allow a user the option to: Enter a series of words (including blanks) and encoded them into their equivalent Morse code. Enter a series of encoded Morse code and translate it into its equivalent letters, digits and punctuation marks. Allow the user to keep executing the application multiple times (i.e. encode than decode or vice versa). Some sort of loop depending on when the user chooses to quit? (User may want to simply encode then decode then return to decoding. Part of your grade is to allow a workable interface from the user's standpoint) Quit the program. Accuracy counts! (use either a "dash" and be consistent, period is the "dot")Explanation / Answer
// Header file section
#include <iostream>
#include <string>
using namespace std;
// Function prototype
string getCode(char);
// main function
int main()
{
// declare variables
string userString;
string marseCode = "";
Prompt the user input any string to convert into marse code.
// prompt the user input
cout << "Enter a string: ";
getline(cin, userString);
Get the characters from the first to last position of the given string.
// get the characters from given string
for(int index = 0; index < userString.length(); index++)
{
/* get the character at index position from the given string */
char ch = userString.at(index);
Convert the character to uppercase.
// convert the character to uppercase
char upper = toupper(ch);
Call the getCode function to get the marse code of the character.
/* call the getCode function to get the marse code of the character */
string str = getCode(upper);
Append the marse code of the character.
// append the marse code of the character
marseCode.append(str);
}
Display the marse code of the given string.
// display the marse code of the given string
cout << " The marse code of the given string is: " << marseCode << endl;
// pause system for a while
system("pause");
return 0;
} // end or main function
// Get the code for the character
string getCode(char ch)
{
switch(ch)
{
case ' ':
return " ";
case ',':
return "--..--";
case '.':
return ".-.-.-";
case '?':
return "..--..";
case '0':
return "-----";
case '1':
return ".----";
case '2':
return "..---";
case '3':
return "...--";
case '4':
return "....-";
case '5':
return ".....";
case '6':
return "-....";
case '7':
return "--...";
case '8':
return "---..";
case '9':
return "----.";
case 'A':
return ".-";
case 'B':
return "-...";
case 'C':
return "-.-.";
case 'D':
return "-..";
case 'E':
return ".";
case 'F':
return "..-.";
case 'G':
return "--.";
case 'H':
return "....";
case 'I':
return "..";
case 'J':
return ".---";
case 'K':
return "-.-";
case 'L':
return ".-..";
case 'M':
return "--";
case 'N':
return "-.";
case 'O':
return "---";
case 'P':
return ".--.";
case 'Q':
return "--.-";
case 'R':
return ".-.";
case 'S':
return "...";
case 'T':
return "-";
case 'U':
return "..-";
case 'V':
return "...-";
case 'W':
return ".--";
case 'X':
return "-..-";
case 'Y':
return "-.--";
case 'Z':
return "--..";
}
} // end of getCode function
Output:
Enter a string: j,o.h n3?
The marse code of the given string is:
.-----..-----.-.-.-.... -....--..--..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.