@lakshayarora7/

BurdensomeSolidJabiru

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## Implement a Caesar Cipher that shifts all the letters in a given message by an given number of places.

## Example of Caesar Cipher: 'Hello' becomes 'Khoor' when characters are shifted by 3 places. 'Can you read this?' becomes 'Kiv gwc zmil bpqa?' when characters are shifted by 8 places. 'Can you read this?' becomes 'Zxk vlr obxa qefp?' when characters are shifted by 3 places in the backwards direction.

## Julius Caesar was a Roman dictator who came up with a clever way of passing confidential information to his generals without having to worry about it falling into the wrong hands. He shifted each letter in the message by a fixed shift value i.e. if the shift value was 3, a became d, f became i and so on. He would leave the numbers and other characters untouched. His generals would only need the shift value to decode their emperor's message. Later on, this cryptography technique came to be known as the Caesar Cipher.

## THOUGHT PROCESS: Prompt the user for the message to encrypted and the number of places each character should be shifted by in the encrypted message -> initialize the encrypted message to an empty string -> begin a for loop to process each character in the entered message -> branch the control into three code blocks, to process uppercase, lowercase and non-alphabet characters separately -> for uppercase and lowercase characters, calculate the position of the character by subtracting Unicode value of the first letter i.e. 'a' or 'A' from the Unicode value of the character -> calculate its new position by adding the shift value and obtaining the remainder on dividing by 26 -> convert the new position into character form using the builtin chr() function -> append the new character to the encrypted message -> for non-alphabet characters, append them to the encrypted message without shifting -> output the encrypted message.

## INPUT
message = input("Enter the message you want to encrypt: ")
shiftValue = int(input("Enter the places you want the characters to be shifted by: "))

## PSEUDO CODE / ALGORITHM:
# Initialize the newMessage to an empty string
# Using a for loop, process each letter in the following way:
#     Determine whether it's lowercase or uppercase using an if statement, proceed accordingly:
#         using ord(), determine its position in the alphabet(0-25)
#         calculate its new offset by adding the shiftValue and getting the remainder on dividing by 26(for taking care of shiftValue > 26)
#         convert the position to character by using chr()
#         append the new character to the newMessage
#     If the character is not an alphabet, append it to newMessage as it is, without shifting.
newMessage = ""
for character in message:
    if character >= "a" and character <= "z":
        position = ord(character) - ord("a")
        position = (position + shiftValue) % 26
        newCharacter = chr(position + ord("a"))
        newMessage = newMessage + newCharacter
    elif character >= "A" and character <= "Z":
        position = ord(character) - ord("A")
        position = (position + shiftValue) % 26
        newCharacter = chr(position + ord("A"))
        newMessage = newMessage + newCharacter
    else: 
        newMessage = newMessage + character

## OUTPUTTING RESULT
print("\nHere is the encrypted message: \n\n" + newMessage)