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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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 Mar 2026

Contest runs March 3 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.