Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-013

Caesar Cipher

Problem

Apply a Caesar shift of k positions to text, preserving letter case and leaving non-letter characters unchanged.

Letters wrap around the alphabet, so shifting z by 1 gives a, and k may be negative to shift backwards. Return the transformed string.

Hint

Use modulo 26 on the 0 to 25 letter index, separate for lowercase and uppercase.

Test cases

case 1
in: ["abc",3]
expected: "def"
case 2
in: ["xyz",3]
expected: "abc"
case 3
in: ["Hello, World!",5]
expected: "Mjqqt, Btwqi!"
case 4
in: ["abc",-3]
expected: "xyz"
case 5
in: ["",10]
expected: ""
Starter codePython
def caesar_shift(text, k):
    # Your code here
    pass

5 test cases · run your code against them in the editor