Question

The task at hand is to create a C++ program (without using any templates) to encode...

The task at hand is to create a C++ program (without using any templates) to encode files and strings using the rot13 encoding method. You should implement the rot13 algorithm using functions, strings and file I/O. Your 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. You will need to provide the file I/O and the functions that implement the rot13 algorithm.
Note: rot13 is a very specific version of the Caesar cipher. The rot13 algorithm simply rotates each individual alphabetic character by 13 characters. It leaves the numbers, punctuation, etc alone. To "encrypt", you rotate each character by 13 characters. To "decrypt", you just rotate (in either direction) the characters by 13 as well. Preserve the case of the rotated letter.

Hint: 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.

Here's an example:
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 and then convert back to lowercase.

Here's another example:
Assuming your input file contains the following text:
The quick brown fox jumps over the moon!
Your encrypted file would be:
Gur dhvpx oebja sbk whzcf bire gur zbba!
Note: The decrypted text should be the same as the original file text.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#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 + "\n";

}

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.txt");

str = read_file("out_encode.txt");

string dec_str = decode(str);

write_file(dec_str, "out_decode.txt");

return 0;

}

Add a comment
Know the answer?
Add Answer to:
The task at hand is to create a C++ program (without using any templates) to encode...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT