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