Hone logo
Hone
Problems

Decoding a Caesar Cipher Message

Imagine you've intercepted a message encrypted using a Caesar cipher, a simple substitution cipher where each letter is shifted a certain number of positions down the alphabet. Your task is to write a program that decodes this message, given the ciphertext and the shift value. This is a fundamental problem in cryptography and a good exercise in string manipulation and modular arithmetic.

Problem Description

You are given a string ciphertext representing the encrypted message and an integer shift representing the number of positions each letter has been shifted. Your goal is to decode the message by shifting each letter back by shift positions. Assume the alphabet is circular (i.e., 'a' comes after 'z'). Non-alphabetic characters (spaces, punctuation, numbers) should remain unchanged. The shift value can be positive or negative.

What needs to be achieved:

  • Decode the Caesar cipher message.
  • Handle both uppercase and lowercase letters.
  • Preserve non-alphabetic characters.
  • Account for circular alphabet wrapping.

Key Requirements:

  • The input ciphertext will be a string.
  • The input shift will be an integer.
  • The output should be a string representing the decoded message.

Expected Behavior:

The program should iterate through the ciphertext string. For each character:

  1. If the character is an uppercase letter (A-Z):
    • Calculate the decoded character's ASCII value by subtracting the shift value from the original character's ASCII value.
    • Apply modular arithmetic (26) to handle wrapping around the alphabet.
    • Convert the ASCII value back to a character.
  2. If the character is a lowercase letter (a-z):
    • Calculate the decoded character's ASCII value by subtracting the shift value from the original character's ASCII value.
    • Apply modular arithmetic (26) to handle wrapping around the alphabet.
    • Convert the ASCII value back to a character.
  3. If the character is not an alphabetic character, leave it unchanged.

Edge Cases to Consider:

  • Empty ciphertext string.
  • shift value of 0 (no shift).
  • Large positive or negative shift values (e.g., shift > 26 or shift < -26). These should still wrap correctly due to modular arithmetic.
  • ciphertext containing non-ASCII characters (consider how you want to handle these – for simplicity, assume they are ignored or unchanged).

Examples

Example 1:

Input: ciphertext = "Lipps Asvph!", shift = 4
Output: "Hello World!"
Explanation: Each letter is shifted back 4 positions in the alphabet. Non-alphabetic characters remain unchanged.

Example 2:

Input: ciphertext = "Bzdrzq!", shift = -3
Output: "Hello World!"
Explanation: Each letter is shifted forward 3 positions in the alphabet (equivalent to shifting back -3).

Example 3:

Input: ciphertext = "Mjqqt, Btwqi!", shift = 5
Output: "Hello, World!"
Explanation: Demonstrates handling of punctuation and spaces.

Constraints

  • ciphertext will be a string with a maximum length of 1000 characters.
  • shift will be an integer between -26 and 26 (inclusive).
  • The input string will only contain ASCII characters.
  • The solution should decode the message efficiently, ideally in O(n) time, where n is the length of the ciphertext.

Notes

  • Consider using the ASCII values of characters to perform the shifting and wrapping.
  • Modular arithmetic (using the modulo operator %) is crucial for handling the circular nature of the alphabet.
  • You can use helper functions to separate the logic for handling uppercase and lowercase letters.
  • Think about how to handle cases where the shift value is larger than the alphabet size. The modulo operator will automatically handle this.

Pseudocode:

FUNCTION decode_caesar_cipher(ciphertext, shift):
  decoded_message = ""
  FOR EACH character IN ciphertext:
    IF character IS an uppercase letter:
      decoded_char_ascii = (ASCII_value(character) - shift - 65) % 26 + 65
      decoded_message += CHARACTER(decoded_char_ascii)
    ELSE IF character IS a lowercase letter:
      decoded_char_ascii = (ASCII_value(character) - shift - 97) % 26 + 97
      decoded_message += CHARACTER(decoded_char_ascii)
    ELSE:
      decoded_message += character
  END FOR
  RETURN decoded_message
Loading editor...
plaintext