0

I have school project to write program which should translate text to morse code and also morse code to string. Down here is my code which translates just text to morse, but I have no idea how to translate morse code to text. Any ideas? I tried to solve it using for cycles but it didn't work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace morse
{
    class Program
    {   
        static void Main(string[] args)
        {
            char[] text = { ' ', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
            string[] TranslatedMorse = { "    |", " . - |", " - . . . |", " - . - . |", " - . . |", " . |", " . . - . |" 
                    , " - - . |", " . . . . |", " . . |", " . - - - |", " - . - |", " . - . . |", " - - |",
                " - . |", " - - - |", " . - - . |", " - - . - |", " . - . |", " . . . |", " _ |", " . . - |",
                " . . . - |", " . - - |", " - . . - |", " - . - - |", " - - . . |", " . - - - - |",
                " . . - - - |", " . . . - - |", " . . . . - |", " . . . . . |", " - . . . . |", " - - . . . |",
                " - - - . . |", " - - - - . |", " - - - - - |" };
            Console.WriteLine("Choose translation:");
            Console.WriteLine("[1] String to Morse");
            Console.WriteLine("[2] Morse to String");
            int.TryParse(Console.ReadLine(), out int selection); 
            Console.WriteLine($"You've choosen: [{selection}]");
            string ToTranslate = "";
            string TranslatedText = "";
            if (selection == 1) 
            {
                Console.WriteLine("Enter text:");
                ToTranslate = Console.ReadLine(); 
                ToTranslate = ToTranslate.ToLower(); 
                for (int i = 0; i < ToTranslate.Length; i++)
                {
                    for (int j = 0; j < 37; j++)
                    {
                        if (ToTranslate[i] == text[j])
                        {
                            TranslatedText += TranslatedMorse[j];
                            break;
                        }
                    }
                }
                Console.WriteLine($"Translated text: {TranslatedText}");

            }
            else if (selection == 2)
            {
                Console.WriteLine("Enter morse code using '.' or '-', split each letter by space, whole words split by two spaces");
                ToTranslate = Console.ReadLine();
                //This I need to solve.
            }
            else
            {
                Console.WriteLine("Nothing selected, program will shut down");
                System.Environment.Exit(1);
            }
        }    
    }
}
Vít Michenka
  • 11
  • 1
  • 2
  • 3
    What does "it didn´t work" mean? Any exceptions? Unexpected results? Please be more specific on your inout and the expected behaviour as well as the actual results you get instead. – MakePeaceGreatAgain Dec 09 '19 at 11:51
  • 4
    Just a small suggestion, you can simplify your code a lot if you use a `Dictionary` instead of `char[]` text and `string[] TranslatedMorse ` – MikNiller Dec 09 '19 at 12:00
  • So your first task is to split the input into ".-" sequences each representing a character. If this is done, look up each code (you have the codes already in TranslatedMorse, though with a different formatting). – Klaus Gütter Dec 09 '19 at 12:00
  • How about asking your teacher or peers for help if it's a school project? – Mike Dec 09 '19 at 12:09
  • Does this answer your question? [Decode Morse without white spaces to text](https://stackoverflow.com/questions/22418182/decode-morse-without-white-spaces-to-text) – Renat Dec 09 '19 at 12:11

1 Answers1

0

Take your morse string and use the Split function to separate the string into an array of strings using the | delimiter. Then you can check each element to find the corresponding letter.

Though on StackOverflow they expect you to make a bigger effort before asking. It helps to break the problem up into very small pieces rather than looking at the whole problem of translating from x to y. In other words, see how would you do it as a human being before you code it and then try to do that in code. Also if teachers aren't a help you might consider working in teams with other students to make more progress (it's also more fun).

JerryTheGreek
  • 160
  • 2
  • 7