-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In python
- Loading branch information
0 parents
commit bc6c287
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |