Used In: SRM 147
Used As: Division II Level One
Problem Statement
Julius Caesar used a system of cryptography, now known as Caesar Cipher, which shifted each letter 2 places further through the alphabet (e.g. 'A' shifts to 'C', 'R' shifts to 'T', etc.). At the end of the alphabet we wrap around, that is 'Y' shifts to 'A'.
We can, of course, try shifting by any number. Given an encoded text and a number of places to shift, decode it.
For example, "TOPCODER" shifted by 2 places will be encoded as "VQREQFGT". In other words, if given (quotes for clarity) "VQREQFGT" and 2 as input, you will return "TOPCODER". See example 0 below.
Definition
Class: CCipher
Method: decode
Parameters: String, int
Returns: String
Method signature: String decode(String cipherText, int shift)
(be sure your method is public)
Constraints
- cipherText has between 0 to 50 characters inclusive
- each character of cipherText is an uppercase letter 'A'-'Z'
- shift is between 0 and 25 inclusive
Examples
"VQREQFGT"
2
Returns: "TOPCODER""ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10
Returns: "QRSTUVWXYZABCDEFGHIJKLMNOP""TOPCODER"
0
Returns: "TOPCODER""ZWBGLZ"
25
Returns: "AXCHMA""DBNPCBQ"
1
Returns: "CAMOBAP""LIPPSASVPH"
4
Returns: "HELLOWORLD"
#include <iostream>
#include <string>
using namespace std;
class CCipher {
private:
int mod(int n, int m) {
return n % m < 0 ? n % m + m : n % m;
}
public:
string decode(string cipherText, int shift) {
string original = "";
for (int i = 0; i < cipherText.size(); ++i) {
original += 'A' + mod(cipherText[i] - 'A' - shift, 26);
}
return original;
}
};