Code Friday/Challenge 6: Code Golf: Caesar's Cipher


You can learn a lot about how a programming language and its compiler interact by "pushing the limits" with challenges like "Code Golf". Code Golf is best described as attempting to write a program to perform a specified task while using the least amount of code possible. The resulting program is unlikely to be easy to read, but learning how to do tasks in the shortest possible way can teach you a lot about how a programming language works, and it is fun!


Using the fewest characters possible, write a valid C++ program, complying with the C++ 11 standard, that will employ Caesar's Cipher to encrypt or decrypt a phrase given on the command line. The program must accept the following items as command-line arguments in exactly the order shown:

  1. The word encrypt or decrypt, indicating the action that the user wishes to perform.
  2. A single alphabetical character representing the key for the cipher. For example, encrypting with the key d would shift the alphabet by 3 letters. The letter 'a' would become 'd', and the letter 'z' would "wrap around" to become 'c'.
  3. A message consisting of no less than one character but up to as many words as the user wants or the terminal allows (no theoretical limit, although a hardware limit will exist, and may vary).

The program will then either encrypt or decrypt the message using the key, and the results will be output to stdout (the standard console output stream).


The following shows an example session in which a secret message is encrypted and then decrypted:

Handling Non-Alphabetical Characters

The message may contain non-alphabetical characters (like the spaces in the example above); if it does, those must be passed through unchanged by the encryption/decryption operation.

Error Handling Requirements:

For the purposes of the Code Golf Challenge, you are not required to check for basic failure conditions; you may make the following assumptions:

  • Your program will always be invoked with a legal set of arguments (as described above).
  • You need only check the first letter of the encrypt or decrypt command to determine which action to take.
  • The key (and message) will always be lowercase.


You may use any "tricks" or language features you like to solve this problem, provided you adhere to the following rules:

  • Your program must compile and run on a Linux (Ubuntu) system with the gcc 5.1.0 compiler using the command and compiler flags shown below:

g++ -Wall -Wextra -std=c++11 -pedantic programName.cpp

Where programName.cpp is replaced by your actual program name. No other flags or options may be added.

  • Your program must not produce any compiler warnings when compiled.
  • Your program must end with the newline character as the last character in the file.
  • Your program must not omit the return 0; required by the C++ standard (even though the compiler will not flag it as an error).
  • No input is allowed from any source. (All incoming data must be obtained only through the command-line parameter list.)
  • Your program must run to successful completion (no crashes, segmentation faults, exceptions, or infinite loops allowed).
  • You may not include any non-standard headers, or link against any non-standard libraries.

Parting Thoughts

Write the code in a readable format first, then carefully remove extra characters like whitespace. Remember, every character counts! Any trick you can use to reduce the number of characters in your source code is fair game (except those tricks disallowed by the limitations above).

Have fun!