Assignment 6 - ldkvd/CS101L GitHub Wiki

Welcome to the CS101L Wiki!

Caesar Cipher

The Caesar Cipher is an encryption/decryption method that shifts the alphabet. For instance, if you have a cipher that shifts by 1, A would become B, B would become C, Z would wrap and become A. To decrypt that cipher you simply shift by -1. It is a very simple cipher that is easily broken. http://en.wikipedia.org/wiki/Caesar_cipherWe need you to write a utility that Encodes and Decodes a Cipher.

The Rules

  • The main program loop should ask the user if they want to:
  1. Encode a string
  2. Decode a string
  3. Quit
  • If the user chooses anything other than 1, 2, or 3, they get an error and it tries again.
  • If they want to encrypt a word or sentence, ask for the phrase and then ask for the # to shift by, once you encrypt the string, output the result to the user.
  • If they want to decrypt a word or sentence, ask for the string and the # of to shift by. Then display the decrypted string to the user.
  • Go back to the main menu until they select quit.

Answers: Image Code 1 Image Code 2 Image Code 3 Image Output

In this code, multiple functions are defined and called. Functions allow for the same piece of code to run multiple times. It reduces clutters, complexity, and duplication of codes. It breaks down a large program into smaller, easy-to-manage components. We can call one statement (the function), rather than writing the same code over and over for each time we want to encode or decode a plain text.

The while loop will continue the program until the user enters an input that will break the loop and quit the program. The function Encrypt() will encrypt the user's message. There is an empty string to hold the new shifted characters. First, it will iterate through the text. If it is a character, then it will convert it to an integer and add by how much the user wants to shift. The newly shifted integer will be converted to a character and added to the string variable. If it is a space, then it will just be added to the string variable. When this function is called, it will return the string variable. The function Decrypt() will decrypt the user's message. Just like the function Encrypt() the process to decrypt is the same. However, this time, instead of adding by how much the user wants to shift, it will subtract by how much the user wants to shift. The function Print_Menu() will print the possible selections that the user can choose from. The function Get_Input() will ask the user to input the number for the selection that they want to do. When a selection is made, the program will run through the if-else statments and perform the run one. If a user selects "1", then the program will enter the if statement and ask the user the text they would like to encrypt and by how many shifts. It calls the Encrypt() functions and displays the newly encoded message. If a user selects "2", then the program will enter the elif statement and ask the user the text they would like to decrypt and by how many shifts. It calls the Encrypt() functions and displays the newly decoded message. If a user selects "3", then the program will display a message and quit.