Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding Morse Code to Text

When I try to replace the the inputed morse code into text I get a problem. Basically the problem is that when the user inputs the ".", the prorgram prints the letter "E"(which is "." in morse code) and ignores all of the other periods after it to make other letters with two or more consecutive periods.

How should I solve this?

I know that this is probably a very newbie question but I've been struggling all day to find an answer for this.

Here's the code

public partial class Morsetext : PhoneApplicationPage
{

    public string[] aakkoset = { ".", "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", "Ä", "Ö", "0", "1", 
                                 "2", "3", "4", "5", "6", 
                                 "7", "8", "9", "?", ":",
                                 ",", "@", "/", "=", " ",
                                 };
    public string[] morse = {".-.-.-", ".-", "-...", "-.-.", "-..", ".", 
                              "..-.", "--.", ".... ", "..", ".---", 
                              "-.-", ".-..", "--", "-.", "---", 
                              ".--.", "--.-", ".-.", "...", "-", 
                              "..-", "...-", ".--", "-..-", "-.--", 
                              "--..", ".-.-", "---.", "-----", ".----", 
                              "..---", "...--", "....-", ".....", "-....", 
                              "--...", "---..","----.", "..--..", "---...",
                              "-....-", ".--.-.", "-..-.", "-...-", " ", 
                              };

    public Morsetext()
    {
        InitializeComponent();
    }

    private void bShort_Click(object sender, RoutedEventArgs e)
    {
        char piste = '.';

        tBoxMorse2.Text += piste.ToString();

    }

    private void tBoxMorse2_TextChanged(object sender, TextChangedEventArgs e)
    {

        tBlockText2.Text =  tBoxMorse2.Text.ToUpper()
            .Replace(morse[0],aakkoset[0])
            .Replace(morse[1],aakkoset[1])
            .Replace(morse[2],aakkoset[2])
            .Replace(morse[3],aakkoset[3])
            .Replace(morse[4],aakkoset[4])
            .Replace(morse[5],aakkoset[5])
            .Replace(morse[6],aakkoset[6])
            .Replace(morse[7],aakkoset[7])
            .Replace(morse[8],aakkoset[8])
            .Replace(morse[9],aakkoset[9])
            .Replace(morse[10],aakkoset[10])
            .Replace(morse[11],aakkoset[11])
            .Replace(morse[12],aakkoset[12])
            .Replace(morse[13],aakkoset[13])
            .Replace(morse[14],aakkoset[14])
            .Replace(morse[15],aakkoset[15])
            .Replace(morse[16],aakkoset[16])
            .Replace(morse[17],aakkoset[17])
            .Replace(morse[18],aakkoset[18])
            .Replace(morse[19],aakkoset[19])
            .Replace(morse[20],aakkoset[20])
            .Replace(morse[21],aakkoset[21])
            .Replace(morse[22],aakkoset[22])
            .Replace(morse[23],aakkoset[23])
            .Replace(morse[24],aakkoset[24])
            .Replace(morse[25],aakkoset[25])
            .Replace(morse[26],aakkoset[26])
            .Replace(morse[27],aakkoset[27])
            .Replace(morse[28],aakkoset[28])
            .Replace(morse[29],aakkoset[29])
            .Replace(morse[30],aakkoset[30])
            .Replace(morse[31],aakkoset[31])
            .Replace(morse[32],aakkoset[32])
            .Replace(morse[33],aakkoset[33])
            .Replace(morse[34],aakkoset[34])
            .Replace(morse[35],aakkoset[35])
            .Replace(morse[36],aakkoset[36])
            .Replace(morse[37],aakkoset[37])
            .Replace(morse[38],aakkoset[38])
            .Replace(morse[39],aakkoset[39])
            .Replace(morse[40],aakkoset[40])
            .Replace(morse[41],aakkoset[41])
            .Replace(morse[42],aakkoset[42])
            .Replace(morse[43],aakkoset[43])
            .Replace(morse[44],aakkoset[44]);

    }
like image 562
Allan Haapalainen Avatar asked Aug 16 '26 00:08

Allan Haapalainen


2 Answers

Morse code can be decoded using dichotomic search. The decision tree looks something like this

Morse code

like image 145
parapura rajkumar Avatar answered Aug 19 '26 00:08

parapura rajkumar


The simplest solution is to reorder your replaces from longest morse string first to shortest morse string last.

like image 31
JamieSee Avatar answered Aug 19 '26 01:08

JamieSee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!