current position:Home>Implementation of Morse cipher translator using Python program

Implementation of Morse cipher translator using Python program

2022-02-01 18:09:39 aikutwo

Morse code is a text message as a series of on-off tones 、 Method of light or click transmission , No special equipment required , A familiar little partner can translate directly . It was invented by the telegraph Samuel F. B. Morse Name of .


The algorithm is very simple . Every character in English is a series of “ spot ” and “ Dashes ” Instead of , Or sometimes just singular “ spot ” or “ Dashes ”, vice versa .


1. In the case of encryption , We extract each character from the word one at a time ( If it's not a space ), And match it with the corresponding Morse password stored in any data structure we choose ( If you use python code , Dictionaries can become very useful in this case )

2. Store the Morse password in a variable , This variable will contain the string we encoded , Then we add a space to the string containing the result .

3. When encoding with Morse code , We need to add... Between each character 1 A space , Add... Between each word 2 Consecutive spaces .

4. If the character is a space , Add another space to the variable containing the result . We repeat the process , Until we traverse the entire string


1. In the case of decryption , We first add a space at the end of the string to be decoded ( This will be explained later ).

2. Now let's continue to extract characters from the string , Until we don't have any space .

3. Once we get a space , We will extract the character sequence ( Or our Morse code ) Find the corresponding English characters in , And add it to the variable that will store the result .

4. please remember , Trace space is the most important part of this decryption process . Once we get 2 Consecutive spaces , We will add another space to the variable containing the decoded string .

5. The last space at the end of the string will help us identify the last sequence of Morse code characters ( Because spaces act as checks to extract characters and start decoding them ).


Python Provides a data structure called a dictionary , It stores information in the form of key value pairs , This is very convenient for implementing passwords such as Morse code . We can save the Morse code table in the dictionary , among ( Key value pair )=>( English characters - Morse code ) . Plaintext ( English characters ) Replace the key , Ciphertext ( Morse code ) Form the value of the corresponding key . The value of the key can be accessed from the dictionary , Just as we access the value of an array through an index , vice versa .

Morse code comparison table





image.png Students interested in learning more new programming skills Video link Keep learning ~

Output :

image.png The above is the whole content of this article , You can learn more about programming and Morse code through this program , Welcome everyone to actively interact and exchange , Learn together and make progress together ~

copyright notice
author[aikutwo],Please bring the original link to reprint, thank you.

Random recommended