Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following C++ program. It reads a sequence of strings from the user and uses rot13 encryption to generate output strings. Rot13 is an

Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page.

#include  #include  using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 + ch - 'a') % 26 + 'a'); else if ((ch >= 'A') && (ch <= 'Z')) return char((13 + ch - 'A') % 26 + 'A'); else return ch; } string rot13(string str) { for (unsigned int i = 0; i < str.length(); i++) str[i] = rot13(str[i]); return str; } int main() { string word; while (cin >> word) cout << rot13(word) << " "; return 0; } 

Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages.

Step 2: Run your program and type in "Hello dad, how is mom today?". You should see a sequence of strange characters that look even worse than Latin. This is your encrypted message. Now copy/paste this encrypted message back into your program input. Hopefully you will see your original message. Hit ^D or ^C to kill the program.

Step 3: Your first task is to modify the program to read words from an input file called "message.in" and write encrypted words to cout. To do this, you will need to add "#include " at the top of the file. Then, you need to declare and open your infile and modify the while loop above. You may need to look at an earlier lab to refresh your memory on syntax.

Note: It is essential for the name of the input file to be "message.in" and not some other name. Otherwise the auto grader will not be able to test your program correctly.

Step 4: Use your editor to create a file called "message.in" and copy/paste the following lines into the file.

Ubj pna lbh gryy na rkgebireg sebz na vagebireg ng AFN? Va gur ryringbef, gur rkgebireg ybbxf ng gur BGURE thl'f fubrf. 

Now run your program to decrypt the message above. This is an example of a rot13 joke. These jokes were all the rage in 1980s news groups before we had YouTube and Twitter to amuse us endlessly.

Step 5: Your next task is to modify your program to write encrypted words to a file called "message.out". Again, you will need to declare and open your outfile and modify the while loop above. You may need to look at an earlier lab to refresh your memory on syntax. Compile and run your program. Now you should see the decrypted joke in the "message.out" file.

Step 6: The while loop above reads single words at a time and does a bad job with the format of the output file. Everything ends up on a single line. To fix this, we can read and write single characters at a time. Replace your while loop with the following code. Then compile and run your program to see what happens.

char ch; while (infile.get(ch)) outfile.put( rot13(ch) ); 

The "get" function will read every character in the input file one at a time. It will not skip over white space. Since the rot13 function only transforms letters in the a..z or A..Z range, all of this white space (and punctuation) is written to the output file by the "put" function.

Step 7: To test your modified program, copy/paste the following into your "message.in" file. When you run your program and look at "message.out" you will see another joke that is inspired by the creator of this encryption scheme. Apologies in advance.

Xabpx, Xabpx. Jub'f gurer? Rg. Rg jub? Rg gh, Oehgr? Gura snyy, Pnrfne! 

Step 8: One nice feature of using the "get" function is that we can look at the character and then back up a character using the "unget" function before we do more input/output. For example, if we get a character in the 0..9 range, we can do an unget followed by "infile >> int_variable". On the other hand, if we get a character in the a..z or A..Z range, we can do an unget followed by "infile >> string_variable". Since "get" followed by "unget" is so common, C++ added a "peek" function to look one character ahead in the input without actually reading the character.

Step 9: Character level input/output is great for many applications, but we often want to process files one line at a time. The "getline" function is ideal for this purpose. Do a google search for "c++ getline string" to find the cplusplus documentation for this function. You will see that getline has two parameters. The first parameter is the input stream you wish to read from. The second parameter is a string that will hold the characters you read from the line.

Step 10: Modify your program one more time to replace the while loop with the following code. Then compile and run your program to see what happens. You should see your encrypted message printed on the screen with line numbers.

int count = 1; string line; while (getline(infile, line)) cout << count++ << ":" << rot13(line) << endl; 

Step 11: Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

=+j Enabling a productive global workforce.

Answered: 1 week ago

Question

=+ Are you interested in creating or

Answered: 1 week ago

Question

=+working on a micro-multinational?

Answered: 1 week ago