public static string ToASCII(string morseInput) { StringBuilder output = new StringBuilder(); // Each morse code letter should be delimited by a space. // Each word is delimited by " / " string[] morseWords = morseInput.Split(new[] { " / " }, StringSplitOptions.None); int wordCnt = 0; foreach (string word in morseWords) { string[] morseLetters = word.Split(' '); foreach (string s in morseLetters) { for (int i = 0; i < MorseCodeAlphabet.Count; i++) { MorseCodeLetter morseCodeLetter = MorseCodeAlphabet[i]; if (morseCodeLetter.MorseCodeChars == s) { output.Append(morseCodeLetter.ASCIILetter); break; } } } wordCnt++; if (wordCnt < morseWords.Length) { output.Append(" "); } } return(output.ToString()); }
public static string ToMorse(string input) { StringBuilder output = new StringBuilder(); string[] words = input.Split(' '); int wordCnt = 0; foreach (string word in words) { int letterCnt = 0; foreach (char c in word) { for (int i = 0; i < MorseCodeAlphabet.Count; i++) { MorseCodeLetter morseCodeLetter = MorseCodeAlphabet[i]; if (morseCodeLetter.ASCIILetter == c) { output.Append(morseCodeLetter.MorseCodeChars); if (letterCnt < word.Length - 1) { output.Append(" "); } letterCnt++; break; } } } wordCnt++; if (wordCnt < words.Length) { output.Append(" / "); } } return(output.ToString()); }