Skip to content

Commit

Permalink
Create Caesar's Cipher
Browse files Browse the repository at this point in the history
In python
  • Loading branch information
Hellot-9000 committed Dec 10, 2023
0 parents commit bc6c287
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Caesar's Cipher
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bc6c287

Please sign in to comment.