In question, you will build a program that will encrypt a text file using a tech
ID: 3541130 • Letter: I
Question
In question, you will build a program that will encrypt a text file using a technique known as "Caesar cipher." From Wikipedia:
[Caesar cipher] is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals.
## Program Structure
Given the basic outline in the previous section, develop a program that implements a Caesar cipher. Your application should begin by prompting the user for a text file to encrypt, the desired offset, and an output file. Using the functions described in detail below, encrypt each line of the file using the provided offset and write it to the output file specified by the user.
## Required Functions
Your program should define and use the following functions:
? encode_character(char character, int offset)
o Encodes the supplied single character using the provided offset. Note that non alphabetical characters (spaces, question marks, commas, numbers, etc.) should not be encoded. When attempting to encode a non alphabetical character, simply return character unmodified.
? encode(string text, int offset)
o Encodes the provided text. Note that encode() should call encode_character() for each character of the supplied text.
? main()
o Prompts the user for an input and output file. Calls encode() to encrypt the text found in the input file and writes the resulting encoded text to the output file.
## Sample Output
Here is an example of the program's prompts:
Enter a file name to encode: input.txt
Enter an encoder offset: 3
Enter a destination file for the encoded text: output.txt
Your file has been encrypted.
## The contents of input.txt:
Who is it in the press that calls on me?
I hear a tongue shriller than all the music
Cry "Caesar!" Speak, Caesar is turn'd to hear.
## The contents of output.txt:
Zkr lv lw lq wkh suhvv wkdw fdoov rq ph?
L khdu d wrqjxh vkuloohu wkdq doo wkh pxvlf
Fub "Fdhvdu!" Vshdn, Fdhvdu lv wxuq'g wr khdu.
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
char encode_character(char ch,int offset)
{
if(ch>=97 && ch<=122)
{
int local = ch-'a';
int add = (local+offset)%26;
ch = 'a' + add;
}
else if(ch>=65 && ch<=90)
{
int local = ch-'A';
int add = (local+offset)%26;
ch = 'A' + add;
}
return ch;
}
string encode(string line,int offset)
{
string out;
for(int i=0; i<line.length(); i++)
out+=encode_character(line[i],offset);
return out;
}
int main()
{
ifstream infile;
ofstream outfile;
string line;
string input_file;
string output_file;
int offset;
cout <<"Enter a file name to encode: :";
cin >> input_file;
cout << endl;
infile.open(input_file.c_str());
cout <<"Enter an encoder offset: ";
cin >> offset;
cout << endl;
cout << "Enter a destination file for the encoded text: ";
cin >> output_file;
cout << endl;
outfile.open(output_file.c_str());
if(!infile)
{
cout <<" unable to open input file" << input_file << endl;
return 0;
}
while(!infile.eof())
{
getline(infile,line);
outfile << encode(line,offset)<<endl;
}
infile.close();
outfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.