Introduction
Morse code is a method of encoding text characters as sequences of dots and dashes (or short and long signals). It has historically been used for telecommunication and remains popular for educational and recreational purposes. This program demonstrates how to translate text to Morse code and vice versa using the Go programming language.
Objective
The goal of this program is to take user input as either plain text or Morse code and provide a translation to the other format. This program will help users understand the basics of Morse code and how to programmatically handle text encoding and decoding.
Code
package main import ( "bufio" "fmt" "os" "strings" ) var morseCodeMap = map[string]string{ "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": "-----", " ": "/", } var reverseMorseCodeMap = func() map[string]string { revMap := make(map[string]string) for key, value := range morseCodeMap { revMap[value] = key } return revMap }() func textToMorse(text string) string { text = strings.ToUpper(text) var morse []string for _, char := range text { if code, exists := morseCodeMap[string(char)]; exists { morse = append(morse, code) } else { morse = append(morse, "?") // Unknown character } } return strings.Join(morse, " ") } func morseToText(morse string) string { var text []string for _, code := range strings.Split(morse, " ") { if char, exists := reverseMorseCodeMap[code]; exists { text = append(text, char) } else { text = append(text, "?") // Unknown Morse code } } return strings.Join(text, "") } func main() { reader := bufio.NewReader(os.Stdin) fmt.Println("Morse Code Translator") fmt.Println("Choose an option:") fmt.Println("1. Text to Morse Code") fmt.Println("2. Morse Code to Text") fmt.Print("Enter your choice: ") choice, _ := reader.ReadString('\n') choice = strings.TrimSpace(choice) switch choice { case "1": fmt.Print("Enter text to translate to Morse code: ") text, _ := reader.ReadString('\n') text = strings.TrimSpace(text) fmt.Println("Morse Code:", textToMorse(text)) case "2": fmt.Print("Enter Morse code to translate to text: ") morse, _ := reader.ReadString('\n') morse = strings.TrimSpace(morse) fmt.Println("Text:", morseToText(morse)) default: fmt.Println("Invalid choice. Please run the program again.") } }
Explanation
This program is structured as follows:
- Morse Code Maps: Two maps are used: one for encoding text to Morse code and another for decoding Morse code to text. The reverse map is generated programmatically.
- Translation Functions: The
textToMorse
function translates text to Morse code, and themorseToText
function translates Morse code back to text. - User Input: The program takes user input to determine whether to encode or decode and then processes the input accordingly.
- Error Handling: Unknown characters or Morse codes are replaced with a
?
to indicate invalid input.
How to Run
- Install Go from the official Go website.
- Copy the code into a file named
morse_translator.go
. - Run the program using the command:
go run morse_translator.go
- Follow the on-screen prompts to translate text to and from Morse code.