Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given this empty shell code I need help with this problem. Language is C++ pleas

ID: 3723707 • Letter: G

Question

Given this empty shell code I need help with this problem. Language is C++ please, thank you.

// Place your information here #include <iostream> int main() { return 0; } Part B: Rot13 Encoding/decoding (rotl3.cpp) - Milestone Part B Obiective: The goal of part B is to create a program to encode files and strings using the rot13 encoding method. Information about the rot13 method can be found at http://www.rot13.com Implement the rot13 algorithm using functions, strings and file l/O. You program should use at least 3 to 4 functions. It should be able to read in a text file from a specified filename, encode using rot13, and write it to a specified file. It should be able to decrypt it in the same way. The starter code for rot13 is rot13.cpp which is mostly an empty shell. You will need to provide the file I/O and the functions that implements the rot13 algorithm. Note: rot13 is very specific version of the Caesar cipher. The rot13 algorithm just simply rotates each individual alphabetic character by 13 characters. It leaves the numbers, punctuation, etc alone. To "encrypt" you just rotate each character by 13 characters. To "decrypt" you just rotate (in either direction) the characters by 13 as well. Please preserve the case of the rotated letter. You may want to convert all lowercase to uppercase characters before performing math on them to avoid overflows. You will want to convert them back to lowercase when done. Example 1: Given the letter 'a' the encrypted letter becomes 'a' 13 110 or 'n' Given encrypted letter 'n' than 'n'13 (or 123) is greater than 'z' so it wraps around (in this case back to 'a If using a char data type, the above would "overflow" and become a negative number. It is suggested that you convert lowercase characters to uppercase before performing the rotation

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 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] + 13 > 'Z')

enc_str[i] = 'A' + ((str[i] + 13 - 'Z') % 26) - 1;

else

enc_str[i] = str[i] + 13;

}

else if (str[i] >= 'a' && str[i] <= 'z')

{

if (str[i] + 13 > 'z')

enc_str[i] = 'a' + ((str[i] + 13 - 'z') % 26) - 1;

else

enc_str[i] = str[i] + 13;

}

}

return enc_str;

}

string decode(string str)

{

int i, len = str.length() - 1;

string dec_str = str;

for (i = 0; i < len; i++)

{

if (str[i] >= 'A' && str[i] <= 'Z')

{

if (str[i] - 13 < 'A')

{

dec_str[i] = 'Z' - (('A' - (str[i] - 13)) % 26) + 1;

}

else

dec_str[i] = str[i] - 13;

}

else if (str[i] >= 'a' && str[i] <= 'z')

{

if (str[i] - 13 < 'a')

{

dec_str[i] = 'z' - (('a' - (str[i] - 13)) % 26) + 1;

}

else

dec_str[i] = str[i] - 13;

}

}

return dec_str;

}

void write_file(string str, string filename)

{

ofstream out(filename.c_str());

out << str;

cout << "successfully write into "<<filename << endl;

}

int main()

{

string filename, str;

cout << "Enter filename: ";

cin >> filename;

str = read_file(filename);

string enc_str = encode(str);

write_file(enc_str, "out_encode");

str = read_file("output");

string dec_str = decode(str);

write_file(dec_str, "out_decode");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote