Quiz: Decoding text using Caeser's Cipher

From EECS 182
Quiz: Decoding text using Caeser's Cipher
Jump to: navigation, search

In a Caeser cipher, during encryption or encoding, each character in the input is shifted by a constant amount. For example, if the shift amount is 2, then A -> C, B -> D, C -> E, ..., X -> Z, Y -> A, Z -> B.

With a shift of +2, the text "HELLO" would become "JGNNQ". To decode the string, one would apply the opposite shift. For example, shifting JGNNQ by -2 would give "HELLO". For non-alphabets (e.g., spaces or numbers), we can leave them as is. Your goal is to decode and submit the following string using a C++ program by shifting it by -6:


To get to a general solution that will work for decoding any encoded string, first write a function in C++ that shifts just one character by a given amount:

	char shiftchar(char c, int shift) {
	  // INPUT: a character c
	  // EFFECTS: returns the character after shifting c by shift (which can
		 be positive or negative) if c is between 'A' and 'Z'. 
		 Assume that shift is between -25 to +25. Otherwise, the function
		 just returns c (no shift).

Note that in C++, single characters have the type "char" and specific values are expressed using single quotes. C++ strings use double-quotes. C++ makes a distinction between a character and a string.

Test your function by writing a main() that includes the following assert statements:

	assert(shiftchar('A', 2) == 'C');
	assert(shiftchar('Z', 2) == 'B');
	assert(shiftchar('B', -2) == 'Z');  
	assert(shiftchar(' ', 10) == ' '); // no shift for spaces
	assert(shiftchar(' ', -10) == ' '); // no shift for spaces
	assert(shiftchar('2', 3) == '2');  // no shift for numbers
	assert(shiftchar('L', 1) == 'M');

Don't forget to do #include <assert.h> at the top of the file.

To shift a character, you can just add an integer to it in C++ because C++ stores chars as numbers internally using their ordinal (ascii) values. Here is a small example of C++ code to shift 'A' by 3 and print it:

	char ch = 'A';   // ch gets the ascii value of the character 'A'
	newch = ch + 3; // add 3 to the ascii value of the character
	cout << newch;   // outputs 'D'.

Python Note: In Python, you would have needed to convert a character to its ordinal value by using the ord(c) function and then use chr(ordinalval) to convert the value back to a charcter. For example, ord('A') is 65 and chr(65) is 'A'. Here is the code in Python to do the same thing:

	ch = 'A'
	new_ord = ord(ch) + 3   # convert ch to its ordinal value and add 3
	newch = chr(new_ord)    # convert ordinal value back to a character
	print newch    # prints D

One problem: ('Z' + 1) would not give you an 'A', since ord('Z') is 90 while ord('A') is 65. It gives you the character with ordinal value 91, which happens to be '['. You need to correct for that in your function. You can check if the sum is larger than 'Z' or smaller than 'A'. If it is larger than 'Z', then the amount by which it is larger than 'Z' needs to be added to ('A' - 1) to get the right letter. You should figure out what needs to be done if it is smaller than 'A'.

To determine whether a character is an alphabet, you can simply check if it is between 'A' and 'Z' (inclusive).

For this problem, you can assume that the input only consists of capital alphabets or non-alphabets.

Using your function as a helper, decode the above text string to a readable string by shifting it by -6. Submit the decoded result as the answer for your quiz. Don't include the terminating newline at the end of the string.

Note: To loop through and print characters in a string s, you can do something like the following:

 for (i = 0; i < s.length(); i++) {
		char c = s[i];
		cout << c << endl;

Submit your code in the Explanation section for the quiz.

Personal tools