Question

In: Computer Science

Task CPP Some information on the Internet may be encrypted with a simple algorithm known as...

Task CPP

Some information on the Internet may be encrypted with a simple algorithm known as rot13, which rotates each character by 13 positions in the alphabet. A rot13 cryptogram is a message or word in which each letter is replaced with another letter.

  • rot13 is an example of symmetric key encryption.

For example, the string

    the bird was named squawk

might be scrambled to form

    cin vrjs otz ethns zxqtop

Details and Requirements

  1. The test driver file lab3.cpp contains an incomplete implementation of the cipher as defined as the class Rot13.

  2. To allow user input a string, you need to override the stream extraction operator >> for the Rot13 class. The class contains the string field text that must be filled with the input text.

  3. The input string may only contain Latin lowercase letters and spaces. Write the method valid that checks if the input satisfies the above condition.
    • If the input contains valid characters then the method returns true value, otherwise it returns false.
  4. Write the method encrypt that encodes the input string using rot13 cipher. The encryption procedure replaces every occurrence of the character with the character 13 steps forward in the alphabet (in a circular way) . For example, a with n , b with o , and x with k, and so on.
    • Spaces are not scrambled.
    • You can transform characters to numbers which would allow to perform rotation transformation using addition/subtraction operations.
    • You can find numerical codes for letters in ASCII Table
  5. To output encrypted text on the screen, you need to override the stream insertion operator << for the Rot13 class.

Instructions

  • Use the file lab3.cpp which contains the testing driver code.
  • Implement Rot13 missing methods, so the program can be executed.
  • Test your program with the provided test input cases.

lab3.cpp

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


class Rot13 {
friend ostream& operator<<(ostream&, const Rot13&);
friend istream& operator>>(istream&, Rot13&);

string text;
public:
Rot13();
bool valid();
void encrypt();
};

bool Rot13::valid() {
}

void Rot13::encrypt() {
}

ostream& operator<<(ostream& out, const Rot13& c) {
}

istream& operator>>(istream& in, Rot13& c) {
}

/*
Please enter the text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Invalid input!
Please enter the text: slkgjskjg akjf Adkfjd fsdfj
Invalid input!
Please enter the text: abcdefghijkl mnopqrst uvwxyz
Encoded text: "nopqrstuvwxy zabcdefg hijklm"
*/
int main() {
Rot13 cipher;

cout << "Please enter the text: ";
cin >> cipher;
if (!cipher.valid()) {
cout << "Invalid input!" << endl;
return 1;
}
cipher.encrypt();
cout << "Encoded text: " << cipher << endl;
return 0;
}

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


class Rot13 {
friend ostream& operator<<(ostream&, const Rot13&);
friend istream& operator>>(istream&, Rot13&);

string text;
public:
Rot13();
bool valid();
void encrypt();
};

Rot13::Rot13(){
}

bool Rot13::valid() {
                
        for(int i=0; i<text.length();i++){
                char c = text[i];
                if(('a' >c || 'z'<c) && c!=' '  ) return false; 
        }
        return true;
}

void Rot13::encrypt() {
        
        string result ="";
        
        for(int i=0;i<text.length();i++){
                
                   char c = text[i];
                 
                 if(c!=' ')
                  result += char(int(c+13-97)%26 +97);
                else
                result +=c;
                 
        }
        text = result;
        
        
}

ostream& operator<<(ostream& out, const Rot13& c) {
        
        out<<c.text;
}

istream& operator>>(istream& in, Rot13& c) {

        getline(in,c.text);
}

/*
Please enter the text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Invalid input!
Please enter the text: slkgjskjg akjf Adkfjd fsdfj
Invalid input!
Please enter the text: abcdefghijkl mnopqrst uvwxyz
Encoded text: "nopqrstuvwxy zabcdefg hijklm"
*/
int main() {
Rot13 cipher;

cout << "Please enter the text: ";
cin >> cipher;
if (!cipher.valid()) {
cout << "Invalid input!" << endl;
return 1;
}
cipher.encrypt();
cout << "Encoded text: " << cipher << endl;
return 0;
}

Related Solutions

An algorithm (like a computer program) is a step-by-step process to accomplish some task. True False...
An algorithm (like a computer program) is a step-by-step process to accomplish some task. True False 2 points    QUESTION 2 All components of an array must be of the same type (all int, all double, all string, etc.). True False 2 points    QUESTION 3 A group of objects defined from the same class will all have different properties and different methods. True False 2 points    QUESTION 4 Each instance of an object from a particular class is...
1) The first task is to review some information that might be useful later: a) Write...
1) The first task is to review some information that might be useful later: a) Write a brief definition of the word "quartile" as we have used it in previous weeks. Be sure to provide a citation: _____________________________. b) Write a brief definition of the word "quantile" as it might be used in statistics. Be sure to provide a citation (do not cut and paste... use your own words to summarize what you discovered): ________________________________. c) From within interactive R,...
What do you know about NAFTA? Use the internet to gather some information about NAFTA and...
What do you know about NAFTA? Use the internet to gather some information about NAFTA and why and when it was started. Do you think NAFTA has been successful in its objectives? Why or why not? Make sure you clearly state the source of your information.
What do you know about NAFTA? Use the internet to gather some information about NAFTA and...
What do you know about NAFTA? Use the internet to gather some information about NAFTA and why and when it was started. Do you think NAFTA has been successful in its objectives? Why or why not? Make sure you clearly state the source of your information.
If all health information is confidential, explain why it may still be necessary to mark some...
If all health information is confidential, explain why it may still be necessary to mark some health records as "sensitive" or "restricted."
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT