Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1452424736110170118
deleted 12 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Morse Code Translatorcode translator in C#

I'm writing a Morse code translator for homework in C#. It takes an input from the user and returns the Morse code version of their input. I understand that this code may look horrible, so how could I improve it in terms of efficiency and readability?

The program takes an input from the user and returns the Morse code version of their input.

Morse Code Translator in C#

I'm writing a Morse code translator for homework in C#. I understand that this code may look horrible, so how could I improve it in terms of efficiency and readability?

The program takes an input from the user and returns the Morse code version of their input.

Morse code translator in C#

I'm writing a Morse code translator for homework in C#. It takes an input from the user and returns the Morse code version of their input. I understand that this code may look horrible, so how could I improve it in terms of efficiency and readability?

Source Link
3lliot
  • 422
  • 2
  • 4
  • 12

Morse Code Translator in C#

I'm writing a Morse code translator for homework in C#. I understand that this code may look horrible, so how could I improve it in terms of efficiency and readability?

The program takes an input from the user and returns the Morse code version of their input.

Program.cs

using System;
using System.Collections.Generic;
namespace MorseCodeTranslator
{
 class Program
 {
 static Dictionary<char, string> translator;
 static void Main(string[] args)
 {
 InitialiseDictionary();
 getUserInput();
 }
 private static void InitialiseDictionary()
 {
 char dot = '.';
 char dash = '−';
 translator = new Dictionary<char, string>()
 {
 {'a', string.Concat(dot, dash)},
 {'b', string.Concat(dash, dot, dot, dot)},
 {'c', string.Concat(dash, dot, dash, dot)},
 {'d', string.Concat(dash, dot, dot)},
 {'e', dot.ToString()},
 {'f', string.Concat(dot, dot, dash, dot)},
 {'g', string.Concat(dash, dash, dot)},
 {'h', string.Concat(dot, dot, dot, dot)},
 {'i', string.Concat(dot, dot)},
 {'j', string.Concat(dot, dash, dash, dash)},
 {'k', string.Concat(dash, dot, dash)},
 {'l', string.Concat(dot, dash, dot, dot)},
 {'m', string.Concat(dash, dash)},
 {'n', string.Concat(dash, dot)},
 {'o', string.Concat(dash, dash, dash)},
 {'p', string.Concat(dot, dash, dash, dot)},
 {'q', string.Concat(dash, dash, dot, dash)},
 {'r', string.Concat(dot, dash, dot)},
 {'s', string.Concat(dot, dot, dot)},
 {'t', string.Concat(dash)},
 {'u', string.Concat(dot, dot, dash)},
 {'v', string.Concat(dot, dot, dot, dash)},
 {'w', string.Concat(dot, dash, dash)},
 {'x', string.Concat(dash, dot, dot, dash)},
 {'y', string.Concat(dash, dot, dash, dash)},
 {'z', string.Concat(dash, dash, dot, dot)},
 {'0', string.Concat(dash, dash, dash, dash, dash)},
 {'1', string.Concat(dot, dash, dash, dash, dash)}, 
 {'2', string.Concat(dot, dot, dash, dash, dash)},
 {'3', string.Concat(dot, dot, dot, dash, dash)},
 {'4', string.Concat(dot, dot, dot, dot, dash)},
 {'5', string.Concat(dot, dot, dot, dot, dot)},
 {'6', string.Concat(dash, dot, dot, dot, dot)},
 {'7', string.Concat(dash, dash, dot, dot, dot)},
 {'8', string.Concat(dash, dash, dash, dot, dot)},
 {'9', string.Concat(dash, dash, dash, dash, dot)}
 };
 }
 public static void getUserInput()
 {
 string input;
 Console.WriteLine("What did you want to say?");
 input = Console.ReadLine();
 input = input.ToLower();
 Console.WriteLine("Your output is: " + translate(input));
 Console.WriteLine("Press enter to end.");
 Console.ReadLine();
 }
 private static string translate(string input)
 {
 System.Text.StringBuilder sb = new System.Text.StringBuilder();
 foreach(char character in input)
 {
 if(translator.ContainsKey(character))
 {
 sb.Append(translator[character] + " ");
 } else if (character == ' ')
 {
 sb.Append("/ ");
 } else 
 {
 sb.Append(character + " ");
 }
 }
 return sb.ToString();
 }
 }
}
lang-cs

AltStyle によって変換されたページ (->オリジナル) /