#C5343. Caesar Cipher Messages
Caesar Cipher Messages
Caesar Cipher Messages
You are given multiple test cases. In each test case, you receive a command character (either 'E' for encode or 'D' for decode), a message consisting solely of uppercase English letters, and an integer shift value. Your task is to process each test case using the Caesar Cipher technique. When encoding, each letter c is replaced by the letter computed using the formula: $$\text{Encoded}(c)=\text{chr}(((\text{ord}(c)-\text{ord}('A')+s) \ \% \ 26)+\text{ord}('A'))$$. For decoding, the shift is applied in the opposite direction.
All operations assume that the alphabet wraps around. For example, encoding "XYZ" with a shift of 3 results in "ABC". Similarly, decoding "ABC" with a shift of 3 results in "XYZ".
Implement a program that reads from standard input and writes the results to standard output. Each result should be printed on a new line in the order the test cases appear.
inputFormat
The first line of input contains an integer T
indicating the number of test cases. Each of the following T
lines contains a test case in the format:
E|D MESSAGE SHIFT
Here, E
means encode and D
means decode. MESSAGE
is a string of uppercase letters without spaces, and SHIFT
is a non-negative integer.
outputFormat
For each test case, output the resulting encoded or decoded message on a separate line to standard output.
## sample3
E HELLO 3
D KHOOR 3
E ABC 1
KHOOR
HELLO
BCD
</p>