Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 3 [40%]: Write a C++ program 3.cpp which encrypts and decrypts some input characters using a variation of the Caesar Shifting algorithm detailed below.
Problem 3 [40\%]: Write a C++ program 3.cpp which encrypts and decrypts some input characters using a variation of the Caesar Shifting algorithm detailed below. Input: - A line of input skc1c2c3, where - s is either the character e for encryption, or the character d for decryption - k is an integer with exactly 6 number digits. The left-most 2 digits form key k1. The middle 2 digits form key k2. The right-most 2 digits form key k3. For example, if k is 226963,k1,k2 and k3 become 22,69 and 63 respectively. k1,k2 and k3 will be used for encryption or decryption as described below. - c1c2c3 is a sequence of space separated characters, ended by !, to be encrypted or decrypted. - c1 will be encrypted with k1. c2 will be encrypted with k2. c3 will be encrypted with k3. c4 will be encrypted with k1,c5 will be encrypted with k2, and so on so forth until the last character is reached. Output: - The encrypted/decrypted message ended by !. There is a space between two consecutive characters. Algorithm: To encrypt (decrypt) a letter ci (within the alphabet A-Z or a-z) with a shift of kj positions: 1. Let x be ci 's position in the alphabet ( 0 based), e.g., position of B is 1 and position of g is 6 . 2. For encryption, calculate y=x+kj modulo 26 ; For decryption, calculate y=xkj modulo 26 . Here modulo is equivalent to remainder in mathematics. E.g., 29 modulo 26=3. 3. Let w be the letter corresponding to position y in the alphabet. If ci is in uppercase, the encrypted (decrypted) letter is w in lowercase; otherwise, the encrypted (decrypted) letter is w in uppercase. A character which is not within the alphabet A-Z or a-z will remain unchanged under encryption or decryption. Example. Given letter B and k=3, we have x=1,y=1+3mod26=4, and w=E. As B is in uppercase, the encrypted letter is e. Requirement: - Implement a function CaesarShift() which takes in a char c and an int k, where c is the character to undergo Caesar Shifting and k is the number of positions (can be negative) to shift, and return the processed char after Caesar Shifting. The returned char is c if c is not within the alphabet range. The prototype of the function is given by: char CaesarShift(char c, int k). - You can ONLY use the simple data types char, bool, int, double. In other words, you are not allowed the use other data types or data structures such as strings, arrays, vectors, etc. Sample Test Cases: User inputs are shown in blue. 3_1 e 112233 ! ! 3_2 e 111213 a B c De! LnP o Q! 3_3 d 211322 D e FgH! i j L u! 3_4 e 134023Hello ENG G 1340 / C O M P 2113 ! uSIYC ba ud 1340/z b a m 2113 ! 3_5 d 202202 n 3 V 3 D3 N_MYN 3_SC_N 3LEQQ3 N_MYN 3 ! T3t3h3t_ker3_wa_r3riow 3 I_ qut 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started