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 September 2026 (Sep 16–30)Sponsored by FLXBL

Contest runs September 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In