Skip to content

2023-3: Caesar Salad

A Caesar cipher, also known as a shift cipher, is one of the simplest encryption techniques. In a Caesar cipher, each letter in the plaintext is replaced by a letter some fixed number of positions away in the alphabet, effectively "shifting" the alphabet.

Write a function that:

  • takes a single integer left argument representing the size of the shift
  • takes a character vector right argument representing the plaintext message
  • returns a result with the same shape as the right argument representing the encrypted message

Notes:

  • Use ' ',⎕A as the alphabet
  • You can assume that the plaintext message will contain only characters found in the alphabet.

Hint: The rotate function could be helpful in solving this problem.

Examples:

      4 (your_function) 'HELLO WORLDS'
LIPPSD SVPHW

      ¯4 (your_function) 'HELLO WORLDS' ⍝ negative shifts are okay
DAHHKWSKNH O 

      0 (your_function) 'HELLO WORLDS' ⍝ no shift is okay
HELLO WORLDS

      27 (your_function) 'HELLO WORLDS'
HELLO WORLDS

      ¯10 (your_function) '' ⍝ returns an empty vector

    
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub