Complete the caesarCipher function in the editor below.caesarCipher has the following parameter(s):string s: cleartextint k: the alphabet rotation factorReturnsstring: the encrypted string
Question
Complete the caesarCipher function in the editor below.caesarCipher has the following parameter(s):string s: cleartextint k: the alphabet rotation factorReturnsstring: the encrypted string
Solution
Sure, here is a Python solution for the Caesar Cipher function:
def caesarCipher(s, k):
encrypted_string = ""
for i in s:
if i.isalpha():
ascii_offset = ord('a') if i.islower() else ord('A')
encrypted_string += chr((ord(i) - ascii_offset + k) % 26 + ascii_offset)
else:
encrypted_string += i
return encrypted_string
Here's how it works:
- We initialize an empty string
encrypted_stringto store the result. - We iterate over each character
iin the input strings. - If the character
iis an alphabet letter (checked usingisalpha()), we calculate its encrypted version. We first determine the ASCII value of 'a' or 'A' depending on whetheriis a lowercase or uppercase letter. This is stored inascii_offset. - We then find the ASCII value of
i, subtractascii_offsetfrom it, addk(the rotation factor), and then find the remainder when divided by 26 (the number of letters in the English alphabet). This gives us the ASCII value of the encrypted letter, to which we addascii_offsetto get back into the correct ASCII range for alphabetic characters. We convert this ASCII value back to a character usingchr()and append it toencrypted_string. - If
iis not an alphabet letter, we simply append it toencrypted_stringas it is. - Finally, we return
encrypted_stringas the result.
Similar Questions
Write a Python program to strip a set of characters from a string. Encrypt a given message by “rotating” each letter by a fixed number of places. To rotate a letter means to shift it through the alphabet, wrapping around to the beginning if necessary, so ‘A’ rotated by 3 is ‘D’ and ‘Z’ rotated by 1 is ‘A’. Write a python function called rotate_word() that takes a string and an integer as parameters (to rotate), and returns a new string that contains the letters from the original string rotated by the given number. For example, "cheer" rotated by 7 is "jolly" and "melon" rotated by -10 is "cubed"
Make a function that encrypts a given input with these steps:Input: "apple"Step 1: Reverse the input: "elppa"Step 2: Replace all vowels using the following chart:a => 0e => 1i => 2o => 2u => 3# "1lpp0"Step 3: Add "vit" to the end of the word: "1lpp0vit"Output: "1lpp0vit"NotesAll inputs are strings, no uppercases and all output must be strings.i/p:bananao/p:0n0n0bvit
What is the ciphertext of "software" using a Caesar cipher?
Alice and Bob want to communicate securely over an insecure channel where Evecould be listening. To ensure their message remains confidential, they decide to usea simple substitution cipher for encryption and decryption, where each letter in thealphabet is shifted by a fixed number of places.For this exercise, use a shift of 3 places to the right (a Caesar Cipher). For example,'A' becomes 'D', 'B' becomes 'E', 'Z' becomes 'C', and so on. Spaces and punctuationare not encoded; they remain unchanged.Alice wants to send the following message to Bob: "HELLO WORLD".a. Encrypt Alice's message using the specified shift. Provide the encrypted text.………………………………………………………………………………………………[1]b. Discuss the security of using the Caesar Cipher for encrypting messages.Consider scenarios where Eve knows or does not know the method ofencryption.……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………[1]
You are given a string s and an integer k. Encrypt the string using the following algorithm:For each character c in s, replace c with the kth character after c in the string (in a cyclic manner).Return the encrypted string. Example 1:Input: s = "dart", k = 3Output: "tdar"Explanation:For i = 0, the 3rd character after 'd' is 't'.For i = 1, the 3rd character after 'a' is 'd'.For i = 2, the 3rd character after 'r' is 'a'.For i = 3, the 3rd character after 't' is 'r'.Example 2:Input: s = "aaa", k = 1Output: "aaa"Explanation:As all the characters are the same, the encrypted string will also be the same. Constraints:1 <= s.length <= 1001 <= k <= 104s consists only of lowercase English letters.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.