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);
}
}
}
}