From bc6c287217b58c9c9bc3e0bf94f770b96b795b17 Mon Sep 17 00:00:00 2001 From: Hellot-9000 <119919411+Hellot-9000@users.noreply.github.com> Date: Sat, 9 Dec 2023 19:35:50 -0700 Subject: [PATCH] Create Caesar's Cipher In python --- Caesar's Cipher | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Caesar's Cipher diff --git a/Caesar's Cipher b/Caesar's Cipher new file mode 100644 index 0000000..df0f1b0 --- /dev/null +++ b/Caesar's Cipher @@ -0,0 +1,24 @@ +def caesarCipher(s, k): + result = "" + + for char in s: + if char.isalpha(): + # Determine if the character is uppercase or lowercase + is_upper = char.isupper() + + # Convert the character to its alphabetical index (0-25) + char_index = ord(char.lower()) - ord('a') + + # Apply the Caesar cipher shift + shifted_index = (char_index + k) % 26 + + # Convert the shifted index back to the corresponding character + shifted_char = chr(shifted_index + ord('a')) + + # Restore the original case + result += shifted_char.upper() if is_upper else shifted_char + else: + # If the character is not a letter, keep it unchanged + result += char + + return result