Morse code has been one of the most basic communication protocol and still used to convey SOS
messages and other urgent communication. In Morse code each English alphabet is encoded by a sequence of ’.’ and’-’, see Figure 1 for complete mapping between English alphabets and Morse codes. Your task is to design a program
that can (i) convert any given string into a Morse code sequence and (ii) a morse code sequence to string. You are not authorized to used string data type, however you can use char *
void ConvertIn(char en[]) {
int eng2;
eng2 = strlen(en);
for (int i = 0; i < eng2; i++) {
if (en[i] == ' ')
cout << endl;
else if (en[i] == '0')
cout << "-----" << endl;
else if (en[i] == '1')
cout << ".----" << endl;
else if (en[i] == '2')
cout << "..---" << endl;
else if (en[i] == '3')
cout << "...--" << endl;
else if (en[i] == '4')
cout << "....-" << endl;
else if (en[i] == '5')
cout << "....." << endl;
else if (en[i] == '6')
cout << "-...." << endl;
else if (en[i] == '7')
cout << "--..." << endl;
else if (en[i] == '8')
cout << "---.." << endl;
else if (en[i] == '9')
cout << "----." << endl;
else if (en[i] == 'A' || en[i] == 'a')
cout << ".-" << endl;
else if (en[i] == 'B' || en[i] == 'b')
cout << "-..." << endl;
else if (en[i] == 'C' || en[i] == 'c')
cout << "-.-." << endl;
else if (en[i] == 'D' || en[i] == 'd')
cout << "-.." << endl;
else if (en[i] == 'E' || en[i] == 'e')
cout << "." << endl;
else if (en[i] == 'F' || en[i] == 'f')
cout << "..-." << endl;
else if (en[i] == 'G' || en[i] == 'g')
cout << "--." << endl;
else if (en[i] == 'H' || en[i] == 'h')
cout << "...." << endl;
else if (en[i] == 'I' || en[i] == 'i')
cout << ".." << endl;
else if (en[i] == 'J' || en[i] == 'j')
cout << ".---" << endl;
else if (en[i] == 'K' || en[i] == 'k')
cout << "-.-" << endl;
else if (en[i] == 'L' || en[i] == 'l')
cout << ".-.." << endl;
else if (en[i] == 'M' || en[i] == 'm')
cout << "--" << endl;
else if (en[i] == 'N' || en[i] == 'n')
cout << "-." << endl;
else if (en[i] == 'O' || en[i] == 'o')
cout << "---" << endl;
else if (en[i] == 'P' || en[i] == 'p')
cout << ".--." << endl;
else if (en[i] == 'Q' || en[i] == 'q')
cout << "--.-" << endl;
else if (en[i] == 'R' || en[i] == 'r')
cout << ".-." << endl;
else if (en[i] == 'S' || en[i] == 's')
cout << "..." << endl;
else if (en[i] == 'T' || en[i] == 't')
cout << "-" << endl;
else if (en[i] == 'U' || en[i] == 'u')
cout << "..-" << endl;
else if (en[i] == 'V' || en[i] == 'v')
cout << "...-" << endl;
else if (en[i] == 'W' || en[i] == 'w')
cout << ".--" << endl;
else if (en[i] == 'X' || en[i] == 'x')
cout << "-..-" << endl;
else if (en[i] == 'Y' || en[i] == 'y')
cout << "-.--" << endl;
else if (en[i] == 'Z' || en[i] == 'z')
cout << "--.." << endl;
}
cout << endl;
}
void ConvertFrom(char a[])
{
cin.getline(a, sizeof(a));
char* ptr = a;
int x = strlen(ptr);
for (int i = 0; i < x; i++)
{
if (a[i] == '.' && a[i + 1] == '-')
{
cout << "A";
}
if (a[i + 1] == '-' && a[i + 2] == '.' && a[i + 3] == '.' && a[i + 4] == '.')
{
cout << "B";
}
if (a[i] == '-' && a[i + 1] == '.' && a[i + 2] == '-' && a[i + 3] == '.')
{
cout << "C";
}
if (a[i] == '-' && a[i + 1] == '.' && a[i + 2] == '.')
{
cout << "D";
}
if (a[i] == '.' && a[i] != '.')
{
cout << "E";
}
if (a[i] == '.' && a[i + 1] == '.' && a[i + 2] == '-' && a[i + 3] == '.')
{
cout << "F";
}
if (a[i] == '-' && a[i + 1] == '-' && a[i + 2] == '.')
{
cout << "G";
}
if (a[i] == '.' && a[i + 1] == '.' && a[i + 2] == '.' && a[i + 3] == '.')
{
cout << "H";
}
if (a[i] == '.' && a[i + 1] == '.')
{
cout << "I";
}
if (a[i] == '.' && a[i + 1] == '-' && a[i + 2] == '-' && a[i + 3] == '-')
{
cout << "J";
}
if (a[i] == '-' && a[i + 1] == '.' && a[i + 2] == '-')
{
cout << "K";
}
if (a[i] == '.' && a[i + 1] == '-' && a[i + 2] == '.' && a[i + 3] == '.')
{
cout << "L";
}
if (a[i] == '-' && a[i + 1] == '-')
{
cout << "M";
}
if (a[i] == '-' && a[i + 1] == '.')
{
cout << "N";
}
if (a[i] == '-' && a[i + 1] == '-' && a[i + 2] == '-')
{
cout << "O";
}
if (a[i] == '.' && a[i + 1] == '-' && a[i + 2] == '-' && a[i + 3] == '.')
{
cout << "P";
}
if (a[i] == '-' && a[i + 1] == '-' && a[i + 2] == '.' && a[i + 3] == '-')
{
cout << "Q";
}
if (a[i] == '.' && a[i + 1] == '-' && a[i + 2] == '.')
{
cout << "R";
}
if (a[i] == '.' && a[i + 1] == '.' && a[i + 2] == '.')
{
cout << "S";
}
if (a[i] == '-')
{
cout << "T";
}
if (a[i] == '.' && a[i + 1] == '.' && a[i + 2] == '-')
{
cout << "U";
}
if (a[i] == '.' && a[i + 1] == '.' && a[i + 2] == '.' && a[i + 3] == '-')
{
cout << "V";
}
if (a[i] == '.' && a[i + 1] == '-' && a[i + 2] == '-')
{
cout << "W";
}
if (a[i] == '-' && a[i + 1] == '.' && a[i + 2] == '.' && a[i + 3] == '-')
{
cout << "X";
}
if (a[i] == '-' && a[i + 1] == '.' && a[i + 2] == '-' && a[i + 3] == '-')
{
cout << "Y";
}
if (a[i] == '-' && a[i + 1] == '-' && a[i + 2] == '.' && a[i + 3] == '.')
{
cout << "Z";
}
}
}
int main()
{
int c;
char eng[50];
cout << "What do you want to do?" << endl;
cout << "1)Convert string to morse code" << endl;
cout << "2)Convert morse code to string" << endl;
cin >> c;
if (c == 1)
{
cin >> eng;
ConvertIn(eng);
}
else
{
cin >> eng;
ConvertFrom(eng);
}
return 0;
}
When converting Morse code to a string, not all characters are successfully converted.
Comments
Leave a comment