Given a string and a shift value, encrypt the string using a Caesar cipher. Each letter in the string is shifted by the given number of positions in the alphabet. Non-letter characters remain unchanged. The shift wraps around (for example, shifting 'z' by 1 gives 'a'). Preserve the case of each letter. Return null for null input and empty string for empty input.
Examples:
Input: str = 'abc', shift = 3
Output: 'def'
Explanation: a shifts to d, b shifts to e, c shifts to f
Input: str = 'xyz', shift = 2
Output: 'zab'
Explanation: x shifts to z, y shifts to a, z shifts to b (wraps around)
Input: str = 'Hello World!', shift = 5
Output: 'Mjqqt Btwqi!'
Explanation: Each letter shifts by 5, case preserved, punctuation unchanged
Input: str = null, shift = 3
Output: null
Explanation: Null input returns null
Input: str = '', shift = 5
Output: ''
Explanation: Empty string returns empty string
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.