Question

Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

Instructions:

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 <iostream>
#include <string>
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 <fstream>" 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;
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

Explanation:

Code in C++ is given below

Code until step 7 is commented out.

Code for step 10 is given.

Output is provided at the end of the code

Code in C++:

#include <iostream>

#include <string>

#include <fstream>

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() {

  ifstream infile;

  ofstream outfile;

  

  infile.open("message.in");

  outfile.open("message.out");

  /**

  * If You want to get solution of step 7 then uncomment following code and comment the step 10 code

  */

  /**Step 7 Code*/

  /*char ch;

  while (infile.get(ch))

    outfile.put(rot13(ch));

  */

  /**Step 10 Code*/

  int count=1;

  string line;

  while(getline(infile,line))

   cout<<count++<<":"<<rot13(line)<<endl;

  return 0;

}

OUTPUT as per Step 10:

1:Knock, Knock.

2:Who's there?

3:Et.

4:Et who?

5:Et tu, Brute? Then fall, Caesar!

Please provide the feedback!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...
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
  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    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 <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • Write a program that reads a sentence input from the user. The program should count the...

    Write a program that reads a sentence input from the user. The program should count the number of vowels(a,e,i,o,u) in a sentence. Use the following function to read the sentence. Should use switch statement with toupper. int read_line(char str[],int n) {    int ch, i=0;    while((ch =getchar()) != '\n')        if(1<n)            str[i++]==ch;    str[i] != '\0';    return i; } output should look like: Enter a sentence: and thats the way it is! Your sentence...

  • c++ program that prints joke and its punchline

    nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

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
Active Questions
ADVERTISEMENT