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
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
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