Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
We will learn how to use Python to implement the Latin Alphabet Cipher.
The Latin Alphabet Cipher Encryption Technique is one of the quickest and most straightforward methods of encoding data. It’s essentially a replacement cipher method, in which each letter of a given input is replaced by its matching number as represented alphabetically.
Examples:
Example1:
Input:
Given string =Hello this is BTechGeeks
Output:
The encrypted message of the given string{ Hello this is BTechGeeks }is : 8 5 12 12 15 20 8 9 19 9 19 2 20 5 3 8 7 5 5 11 19
Example2:
Input:
Given string = btechgeeks python
Output:
The encrypted message of the given string{ btechgeeks python }is : 2 20 5 3 8 7 5 5 11 19 16 25 20 8 15 14
- Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words in a Hyphen-Separated Sequence after Sorting them Alphabetically
- Python Program to Rearrange the Letters of a String in Alphabetical Order
- Python Program to Accept a Sentence and Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop
Python Program to Implement the Latin Alphabet Cipher in Python
Below are the ways to implement the Latin Alphabet Cipher in Python.
Method #1: Using For Loop (Static Input)
Approach:
- Give the string as static input(which consists of only alphabets and spaces) and store it in a variable
- Iterate through the characters of the string using For loop.
- We can calculate the ASCII value of the character using the ord() function.
- Now, transform each input string character to its ASCII value and subtract it from the ASCII value of alphabet A for uppercase characters and ‘a’ for lowercase ones.
- The operation is written as ord(givenstring[i])-ord(“A”)+1 for uppercase letters.
- ord(givenstring[i])-ord(“a”)+1 for lowercase letters.
- If the character is space then print it(That is printing space character without endl which makes it to print in next line)
- The Exit of the program.
Below is the implementation:
# Give the string as static input(which consists of only alphabets and spaces) and store it in a variable givenstring = "Hello this is BTechGeeks" print('The encrypted message of the given string{', givenstring, '}is :') # Iterate through the characters of the string using For loop. # We can calculate the ASCII value of the character using the ord() function. for m in givenstring: # Now, transform each input string character to its ASCII value # and subtract it from the ASCII # value of alphabet A for uppercase characters and 'a' for lowercase ones. if (m >= "A" and m <= "Z"): # The operation is written as ord(givenstring[i])-ord(“A”)+1 for uppercase letters. print(ord(m)-ord("A")+1, end=" ") elif (m >= "a" and m <= 'z'): # ord(givenstring[i])-ord(“a”)+1 for lowercase letters. print(ord(m)-ord("a")+1, end=" ") # If the character is space then print it(That is printing # space character without endl which makes it to print in next line) if m == (" "): print(m)
Output:
The encrypted message of the given string{ Hello this is BTechGeeks }is : 8 5 12 12 15 20 8 9 19 9 19 2 20 5 3 8 7 5 5 11 19
Method #2: Using For Loop (User Input)
Approach:
- Give the string as user input(which consists of only alphabets and spaces) using input() and store it in a variable.
- Iterate through the characters of the string using For loop.
- We can calculate the ASCII value of the character using the ord() function.
- Now, transform each input string character to its ASCII value and subtract it from the ASCII value of alphabet A for uppercase characters and ‘a’ for lowercase ones.
- The operation is written as ord(givenstring[i])-ord(“A”)+1 for uppercase letters.
- ord(givenstring[i])-ord(“a”)+1 for lowercase letters.
- If the character is space then print it(That is printing space character without endl which makes it to print in next line)
- The Exit of the program.
Below is the implementation:
# Give the string as user input(which consists of only alphabets and spaces) # using input() and store it in a variable. givenstring = input('Enter some random string = ') print('The encrypted message of the given string{', givenstring, '}is :') # Iterate through the characters of the string using For loop. # We can calculate the ASCII value of the character using the ord() function. for m in givenstring: # Now, transform each input string character to its ASCII value # and subtract it from the ASCII # value of alphabet A for uppercase characters and 'a' for lowercase ones. if (m >= "A" and m <= "Z"): # The operation is written as ord(givenstring[i])-ord(“A”)+1 for uppercase letters. print(ord(m)-ord("A")+1, end=" ") elif (m >= "a" and m <= 'z'): # ord(givenstring[i])-ord(“a”)+1 for lowercase letters. print(ord(m)-ord("a")+1, end=" ") # If the character is space then print it(That is printing # space character without endl which makes it to print in next line) if m == (" "): print(m)
Output:
Enter some random string = btechgeeks python The encrypted message of the given string{ btechgeeks python }is : 2 20 5 3 8 7 5 5 11 19 16 25 20 8 15 14
Related Programs:
- python program to find the factorial of a number
- python program to find the largest among three numbers
- python program to display the multiplication table
- python program to find the sum of natural numbers
- python program to compute the power of a number
- python program to count the number of each vowel
- python program to find the factors of a number