Translating text into Morse code
Morse code, also known as Morse code, is a system for transmitting textual information. It is based on the use of two signals of different duration — dots (short signal) and dashes (long signal). This system was developed by the American inventor Samuel Morse in the first half of the 19th century and became one of the first methods of telegraphic communication.
Each letter of the alphabet, a number, and some punctuation marks are represented by a unique combination of dots and dashes, for example, the letter A is passed as a dot-dash (.–), and the letter B is dash—dot-dot-dot (-...).
A short pause is made between two consecutive dots or dashes inside one character, between in different characters (letters or numbers) — the pause is slightly longer. In turn, the pause between words is even longer.
Morse code was widely used in telegraphy to transmit messages over long distances. It has also found applications in radio communications, especially in the maritime and aviation industries.
Nowadays, Morse code continues to be studied and applied by radio enthusiasts and retro technology enthusiasts, as signal transmission can be carried out using simple devices such as a telegraph key, and even under poor communication conditions, signals can be recognized due to their simplicity.
Now, based on the theoretical foundations of this system described above, we will define a dictionary for Latin letters and Arabic numerals.
# Dictionary for converting characters to Morse code
morse_code = Dict(
'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' => "--..",
'0' => "-----",
'1' => ".----",
'2' => "..---",
'3' => "...--",
'4' => "....-",
'5' => ".....",
'6' => "-....",
'7' => "--...",
'8' => "---..",
'9' => "----.",
' ' => "/" # The space between words is indicated by a slash
)
Let's define a newline function in the Morse code format. This function searches for the meaning in the dictionary. If the value is not found, it leaves it unchanged.
function text_to_morse(text::String)
morse_message = String[]
for char in uppercase(text)
if haskey(morse_code, char)
push!(morse_message, string(morse_code[char])) # Assigning a value from the dictionary
else
push!(morse_message, string(char)) # We leave the original symbol
end
end
return join(morse_message, " ") # Combining the vector elements separated by a space
end
# An example of using the function
input_text = "ENGEE!"
output_morse = text_to_morse(input_text)
println(output_morse)
Conclusion
The results of the demonstration confirm that this function is working correctly. Due to the fact that the exclamation mark does not have a dictionary value, we get this sign at the output.