• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting morse code to english

 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to set up a morse code-english converter by prompting the user for morse-->english translation or english--> morse translation, prompting the user to enter the text to be translated, and then finally translating it. So far, I've been able to do english-->morse code translations because that just involves encoding each individual letter. I'm having trouble with morse-->english translations though because that doesn't just involve decoding each individual character. The problem I'm having is I don't want the program to convert each individual dash and period to a letter, but rather a string of dashes and periods to a letter (..-. = f). I also want "|" to separate each word in morse code. For example, if morse code input is "-- -.-- | -. .- -- ." then translated output should be "my name". I implemented this successfully in the stringToMorse() method, but I don't know how I would do it in the stringToEnglish() method. Any ideas are appreciated.

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all you probably can implement the conversion from letter to morse-code more efficient with an array or Map instead of those if-statements.

For stringToEnglish: split the morse-code on "|" and do the opposite of creating morse-code based on the letter.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and check whether your Morse code is consistent. You have ".- " for a, with a space in, and no spaces for any other letters. That will cause errors.
 
Ashish Schottky
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Khair: You can do something like this:

Create a database /look up table sort of thing for morse<->english
say something like this.

Take the input as a string.
use a counter to count '|' from the current position, make a substring
search for this substring in morse code array,once found, printout the character from corresponding english array or put that in string.
in this way, you can eliminate the if statements for both types of conversions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As suggested earlier, a Map is a far better solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic