Morse Code to English

I need to covert English to Morse code and vise versa. I found English to Morse however I cant figure out Morse Code to English. When it runs it read the first character of Morse Code but not the rest so .- would be outputted as ab.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <cctype>
#include <string>

using namespace std;


string texttomorse(char l)
	{
		string text = "abcdefghijklmnopqrstuvwqyz 1234567890"; //each letter of the alphabet, a space, and numbers 0-9
		string morse[] ={
						" .-"," -..."," -.-."," -..", " .", " ..-.", " --.",
						" ....", " ..", " .---", " -.-", " .-..", " --",
						" -.", " ---", " .--.", " --.-", " .-.", " ...", " -",
						" ..-", " ...-", " .--", " -..-", " -.--", " --..","  ",
						" .----", " ..---", " ...--", " ....-", " .....", " -....",
						" --...", " ---..", " ----.", " -----",// Each letter and number in Morse
						}; 
		int index = text.find(l);//gets character from input and finds its Morse equivalent
			if(index!=-1)
				return morse[index];
			else
				return " ";
    }

string morsetotext(char l)
	{
		string text[] = {"a","b","c","d", "e", "f", "g", "h", "i", "j", "k", "l", "m","n","o","p","q","r","s","t","u","v","w","q","y","z"," ","1","2","3","4","5","6","7","8","9","0"}; //each letter of the alphabet, a space, and numbers 0-9
		string morse={
					".-/ -.../ -.-./ -../ ./ ..-./ --./ ..../ ../ .---/ -.-/ .-../ --/ -./ ---/ .--./ --.-/ .-./ .../ -/ ..-/ ...-/ .--/ -..-/ -.--/ --../  / .----/ ..---/ ...--/ ....-/ ...../ -..../ --.../ ---../ ----./ -----"// Each letter and number in Morse
				     }; 
		int index = morse.find(l);//gets character from input and finds its Morse equivalent
			if(index!=-1)
				return text[index];
			else
				return " ";
    }
int main ()
{
	int c;
	string userinput;
	
	cout<<"Enter 1 for text to morse. 2 for morse to text\n";
	cout<<"Choice: ";
	cin>>c;
	
	while (c==1)
	{
		getline(cin, userinput);
		string out="";
		for(int i=0; i<userinput.length(); i++)
		{
			out+= texttomorse(userinput[i]);
			
		}
		cout<< out << endl;

		
	}
	while (c==2)
	{
		getline(cin, userinput);
		string out="";
		for(int i=0; i<userinput.length(); i++)
		{
			out+= morsetotext(userinput[i]);
			
		}
		cout<< out << endl;
	}


return 0;
}
Last edited on
It is he way that you put your morse in the string in morsetotext()
I am not sure how to put it in properly
Morse code is a series of strings - 1 to 5 char in length, so it can't be sent to function

string morsetotext(char l)

as a char, plus you can't search string morse to find a meaningful index.

You may have to do a search through an array of morse strings . . .

You'll also need a way to extract the codes from the input string using the spaces to find the breaks between characters.
What can I send it as
Last edited on
The lookup table is a good idea. Here's some help to refine it.

1
2
3
4
5
6
7
8
9
10
11
12
string morse_codes[] =
{
  ".-",    "-...",  "-.-.",  "-..",   ".",     // A-E
  "..-.",  "--.",   "....",  "..",    ".---",  // F-J
  "-.-",   ".-..",  "--",    "-.",    "---",   // K-O
  ".--.",  "--.-",  ".-.",   "...",   "-",     // P-T
  "..-",   "...-",  ".--",   "-..-",  "-.--",  // U-Y
  "--..",                                      // Z
  
  "-----", ".----", "..---", "...--", "....-", // 0-4
  ".....", "-....", "--...", "---..", "----.", // 5-9
};

Notice the total lack of spaces or codes for anything but A-Z, 0-9, in that order. This makes lookup fairly easy both ways.

You'll need functions to handle a single code translation, say 'E'-->"." and "."-->'E'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string English_To_Morse_1( char letter_or_digit )
{
  // Is it a letter?
  if (isalpha( letter_or_digit ))
    // Yes. Convert the letter to uppercase and find it's difference with 'A' to index the correct Morse sequence.
    return morse_codes[ toupper( letter_or_digit ) - 'A' ];

  // Is it a digit?
  if (isdigit( letter_or_digit ))
    // Yes. Find the difference between it an '0' and don't forget the offset in the table of the digits.
    return morse_codes[ 26 + letter_or_digit - '0' ];

  // It is not a letter or a digit, so it turns into... nothing!
  return ""
}

To go the other way, you'll have to find the index of the code sequence in the table, and convert it back into a number or a letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char Morse_To_English_1( string s )
{
  int i = index of s in morse[];  // you can figure out how to do this! (hint: use a loop)

  // Was it a letter?
  if (i < 26)
    // Yes. Add the offset to 'A' to get the correct letter.
    return 'A' + i;

  // Was it a digit?
  if ((i - 26) < 10)
    // Yes. Add the offset to '0' to get the correct digit.
    return '0' + i - 26;

  // It was not a letter or a digit, so it turns into... nothing!
  return ' ';
}

Now that you have functions for individual letters and digits in a message, you can write a pair of functions to convert an entire message:

1
2
3
4
5
6
7
8
9
10
string English_To_Morse( string s )
{
  string result;
  for (int i = 0; i < s.length(); i++)
  {
    result += // convert s[ i ] to a morse sequence. (Hint: use a function!)
    result += ' ';
  }
  return result.substr( 0, result.length() - 1 );  // (skip the last space)
}

Going the other way is very similar, except this time you must split your message up by where the spaces are instead of just each character. This is the most challenging part, actually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string Morse_To_English( string s )
{
  string result;
  int i = 0;

  // So long as we have characters left in s to examine...
  while (i < s.length())
  {
    // Skip all leading spaces
    while ((i < s.length()) && isspace( s[ i ] )) i++;

    // Gather the next code (ending with the next space or the end of the string) into current_code
    string current_code;
    while ((i < s.length()) && !isspace( s[ i ] ))
    {
      current_code += s[ i ];
      i++;
    }

    // At this point, we should have something in current_code that can (potentially) be turned into a letter or digit.
    result += // convert the current code into a letter or digit (Hint: Use the function!)
  }
  return result;
}

Assignments like this kind of tick me off, actually, because they require you to do two things, but typically only one of them is explicitly brought to your attention.

Good job with the lookup table (just use the refinement).
The thing that is not obvious is splitting a string into substrings between spaces.

PS. You don't have to do things like I suggest. But strong hints!

Hope this helps.
The thing that is not obvious is splitting a string into substrings between spaces.


And even less obvious: There is no explicit morse code for a space - normally there is a three-"dit" wait between letters of a word, and a seven-"dit" wait between words.

If your input requires single-spacing between morse code letter elements and double spaces between words you can insert spaces between translated words based on the double-spaces. You'll have to trust the user input to properly frame the words with double spaces, though. I think parsing a run-on line is beyond the scope of what the original problem requires.

Also - there are translation "trees" on-line that could be used in a "fun" way, I'm sure. . .

http://www.learnmorsecode.com/

You could really get the professor's attention by building that in software. . . :)
FORASCHOOLPROJECTIREALLYWOULDNTWORRYABOUTSPACES.

For something more advanced, I'd use a dictionary with basic grammar. (OLE MS Word will do.)
Topic archived. No new replies allowed.