Question: Using C++. A Caesar (or rotation) cipher is a simple system of encoding strings by shifting every letter forward (or backward) by a given amount.

Using C++.

A Caesar (or rotation) cipher is a simple system of encoding strings by shifting every letter forward (or backward) by a given amount. For example, if the shift amount is 3, then the letter A becomes D, B becomes E, C becomes F, and so on. Letters near the end of the alphabet wrap around; for a shift of 3, X becomes A, Y becomes B, Z becomes C.

Write a program that inputs an entire line from the keyboard, followed by an integer shift amount. The program encodes the line and outputs the result. Here are two examples:

Secret message? Attack zerg at dawn Shift amount? 3 Result: DWWDFN CHUJ DW GDZQ Secret message? DWWDFN CHUJ DW GDZQ Shift amount? -3 Result: ATTACK ZERG AT DAWN 

Assume the input consists of letters and spaces only; the letters can be lowercase ('a'-'z') or uppercase ('A'-'Z'). The resulting message should consist of uppercase letters only. You may assume the shift amount is in the range -26 .. 26.

Hints: First, you can use the at() function, or array notation [ ], to access characters in the string, or to change a character:

s.at(i) = 'X'; // s[i] = 'X' also works 

Second, it's very helpful to think of characters as numbers when performing these types of manipulations. Third, you might find the islower() and toupper() functions helpful; these are discussed in the next section. [ Why? Since the result is uppercase, it might be easier to convert to uppercase before performing a shift, that way you only have one case to deal with when shifting. ]

Given Code:

#include #include #include

using namespace std;

int main() { string msg; int amt; cout << "Secret message?" << endl; getline(cin, msg); cout << "Shift amount?" << endl; cin >> amt; // // Your Code: //

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!