Question
Problem 2: The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter
Problem 2:
The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in
which each letter in the plaintext is 'shifted' a certain number of places (to the left or right) down the
alphabet. This directional substitution over a certain number of places is known as the key of the
substitution cipher. For example, with a right-shift of 1, the alphabet A would be replaced by B, B would
become C, and so on. The method is named after Julius Caesar, who apparently used it to communicate
with his generals.
A full example of the Caesar Cipher using a right-shift-1 key would be as follows:
plaintext: defend east wall of the castle
ciphertext: efgfoe uif fbtu xbmm pg uif dbtumf
Without the knowledge of the key, it will be challenging to easily decipher the actual plain text message.
You have been given an input file called myPlainText.txt (see attachment), which consists of a plaintext
message. Write a C++ code that will read the plaintext message from the input file, convert all the upper
case alphabets to lower case and implement the Caesar substitution cipher using the key: left-shift-4. The
generated cipher text should be written to an output file called myCipherText.txt.
Your submission pdf file should contain your C++ source code, compilation, and the cat (linux
command to paste the contents of a file on the terminal) of your output file.
Note: Your code should work for any cipher key with minimalistic changes. The structure of the input
file must be preserved in the output file (The number of lines and words in each line).
(7 points)
HINTS:
1. To convert upper-case alphabet to its lower-case counter-part you can use the in-built function
tolower(variable);
if you have the following variable:
char variable = C;
And you use tolower() as follows:
cout
it will output the value c.
2. Alphabets are associated with numerical values known as ASCII values. The function prototype
of tolower() is as follows:
int tolower(int variable);
As such in point 1 above, If we didnt cast the cout statement using the datatype char the output
would have been the ASCII of lower-case c which is 99. The complete chart of ASCII values
for the alphabets (A,,Z and a,,z) can be found on https://ascii.cl/ . Use this concept when you
design the logic of your program.
3. For a input line as given below:
Hello my name is
If you want to read the entire line in one go including the white spaces (instead of reading word
by word ignoring the whitespaces) then you can use the function getline() which is used as
follows:
#include
//.
int main()
{
string inputString;
ifstream fin;
// open file and validate etc.
while (!fin.eof())
{
getline(fin, inputString);
}
}
The file input stream object will read an entire line into your string variable. You can then
traverse the string character by character by using the following pseudo code:
char c;
for(unsigned int i = 0; i { c = inputString[i]; //this is your character } 4. If you want to open a write file append mode, use following: fout.open(output.txt, ios::app); This is the text file we were given called myPlainText.txt and I am completely lost on using a file as an input and returning an output within a file, please help.
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