Caesar Cipher
Easy
3
68.1% Acceptance
In this lab, you will create two functions, encrypt
and decrypt
, to implement the Caesar cipher technique. These functions should handle both encryption and decryption of a given string based on a user-defined shift value. The primary objectives and constraints for your functions are as follows:
- The functions should be case insensitive, converting all characters to lowercase during encryption and decryption.
- Non-alphabetical characters, including spaces, should not be altered.
Function Requirements
-
encrypt(str, shift)
:- Input: A string (
str
) and an integer (shift
). - Output: The encrypted string, with each alphabetical character shifted forward by
shift
positions in the alphabet.
- Input: A string (
-
decrypt(str, shift)
:- Input: An encrypted string (
str
) and the original shift value (shift
). - Output: The decrypted string, reversing the shift applied during encryption.
- Input: An encrypted string (
Examples
-
Example 1:
- Encrypt:
encrypt("hello world", 5)
should return"mjqqt btwqi"
. - Decrypt:
decrypt("mjqqt btwqi", 5)
should revert back to"hello world"
.
- Encrypt:
-
Example 2:
- Encrypt:
encrypt("Python 3.8!", 12)
should return"dbftbs 3.8!"
. - Decrypt:
decrypt("dbftbs 3.8!", 12)
should revert back to"python 3.8!"
.
- Encrypt:
Your task is to implement these functions accurately and ensure they meet the specified requirements. Pay close attention to case insensitivity and the handling of non-alphabetical characters.