Description A Caesar cipher (http://en.wikipedia.org/wiki/Caesar_cipher) is a si
ID: 3728297 • Letter: D
Question
Description
A Caesar cipher (http://en.wikipedia.org/wiki/Caesar_cipher) is a simple cryptographic technique in which each letter is the original message is shifted to a different letter which is located at a uniform offset in alphabetic order. For example, the message “Hello World!” and offset 3 would result in an encrypted message “Khoor Zruog!” The message “Dogs and Cats” and offset -2 (which is equivalent to an offset of 24) would result in an encrypted message “Bmeq ylb Ayrq”. Note that only alphabetic characters are shifted, and that the case of the letter is preserved.
Functional requirements
Write a Unix program that takes 3 inputs from the command line: the pathname of an existing input file, the pathname of an output file to be created, and an offset between -25 and 25, inclusive. If the input file doesn’t exist, or if the output file cannot be created (perhaps because directories in its path do not exist, or because you don’t have permission to create files in the given location), or if the offset is out of range, the program prints an error message to standard error and immediately ends. Otherwise it uses the input file and offset to create an encoded output file using the Caesar cipher as described above.
Nonfunctional requirements
You may use the C++ string class, fstreams and stringstreams. To convert the 3rd command line argument from a c-string to an int, use stringstreams or the atoi function from stdlib.hhttp://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
string read_file(string filename)
{
ifstream in;
in.open(filename.c_str());
string str = "", inp;
if (in.is_open())
{
while (!in.eof())
{
getline(in, inp);
str += inp + " ";
}
return str;
}
else
{
cout << "Error opening file" << endl;
exit(1);
}
}
string encode(string str, int offset)
{
if(offset<0){
offset = 26+offset;
}
int i, len = str.length() - 1;
string enc_str = str;
for (i = 0; i < len; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
if (str[i] + offset > 'Z')
enc_str[i] = 'A' + ((str[i] + offset - 'Z') % 26) - 1;
else
enc_str[i] = str[i] + offset;
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
if (str[i] + offset > 'z')
enc_str[i] = 'a' + ((str[i] + offset - 'z') % 26) - 1;
else
enc_str[i] = str[i] + offset;
}
}
return enc_str;
}
void write_file(string str, string filename)
{
ofstream out(filename.c_str());
if(out.bad()){
cout<<"Unable to write output"<<endl;
exit(1);
}
out << str;
cout << "successfully write into "<<filename << endl;
}
int main(int argc, char *argv[])
{
if(argc!=4){
cout<<"Invalid Arguments: Ex: ./a.out input output offset"<<endl;
exit(0);
}
string str;
int offset = atoi(argv[3]);
if(offset<=-25 || offset>=25){
cout<<"Invalid offset. range(-25 to 25)"<<endl;
exit(1);
}
str = read_file(argv[1]);
string enc_str = encode(str, offset);
write_file(enc_str, argv[2]);
return 0;
}