#C2392. Caesar Cipher Encryption and Decryption

    ID: 45703 Type: Default 1000ms 256MiB

Caesar Cipher Encryption and Decryption

Caesar Cipher Encryption and Decryption

The Caesar cipher is a classical encryption technique where each letter in the plaintext is shifted by a fixed number of positions. For letters, the transformation follows the formula: \( c = (x - \text{base} + \text{shift}) \mod 26 + \text{base} \), where \( \text{base} \) is either the ASCII code for 'A' (for uppercase) or 'a' (for lowercase). For digits, the similar wrap-around is performed modulo 10: \( c = (x - '0' + \text{shift}) \mod 10 + '0' \).

Your task is to implement both encryption and decryption using the Caesar cipher. Given a text, a shift value, and a direction (either "encrypt" or "decrypt"), output the resulting text after applying the cipher.

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. The first line contains the text to be processed (which may include letters, digits, and other characters).
  2. The second line contains an integer representing the shift value.
  3. The third line contains a string indicating the operation: either "encrypt" to shift forwards or "decrypt" to shift backwards.

outputFormat

Output a single line to standard output (stdout) representing the transformed text after applying the Caesar cipher.## sample

abc
3
encrypt
def