Question

In: Computer Science

This is C++. It's based on structured data, chapter 11 of Starting out with C++. Please...

This is C++. It's based on structured data, chapter 11 of Starting out with C++. Please use basic operations. Please read the requirements carefully and make sure it's executable. The first 2 answers that I received are nowhere close. Thank you.

Secret Codes!

Create a program that will accept a message from the user and either
encrypt ordecrypt it with the following algorithms:

To encrypt:

get the character you wish to encrypt

find the index of that character in the alphabet array

add the shift offset to the index

add the increment to the index

"wrap around" the new index so the result is between 0 and 29

find the character at the new index

To decrypt:

get the character you wish to decrypt

find the index of that character in the alphabet array

subtract the shift offset from the index

subtract the increment from the index

"wrap around" the new index so the result is between 0 and 29

find the character at the new index

repeat steps 1-6 for every shift offset

The alphabet is <space>,a-z(lowercase),<period>,<question mark>,
and <comma> in that order.

The increment should be +3 per letter position.

Here is a sample message. Who said it?

f..yqjrnvrpuabefw.j,ddxgcndpxmsdehsomjkcydfygtd.rrp?dgstmvxmf.wo?jxgrneslzifrgzyqv.v

Solutions

Expert Solution

#include <iostream>
using namespace std;

char c[]= {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z', '.', '?'};
int inc = 3;

string encrypt(string text, int s)
{
string result = "";

// traverse text
for (int i=0;i<text.length();i++)
{
if (islower(text[i]))
result += c[((text[i] - 'a' + 1) + s + inc)%29];
else if (text[i] == ' ')
{
result += c[(0 + s + inc)%29];
}
else if (text[i] == '.')
{
result += c[(27 + s + inc)%29];
}
else if (text[i] == '?')
{
result += c[(28 + s + inc)%29];
}
else
{
result += text[i];
}
}

// Return the resulting string
return result;
}

int wrapIndex(int i, int i_max) {
return ((i % i_max) + i_max) % i_max;
}

string decryptHelper(string text, int s)
{
string result = "";

// traverse text
for (int i=0;i<text.length();i++)
{
if (islower(text[i]))
result += c[wrapIndex((text[i] - 'a' + 1) - s - inc, 29)];
else if (text[i] == ' ')
{
result += c[wrapIndex(0 - s - inc, 29)];
}
else if (text[i] == '.')
{
result += c[wrapIndex(27 - s - inc, 29)];
}
else if (text[i] == '?')
{
result += c[wrapIndex(28 - s - inc, 29)];
}
else
{
result += text[i];
}
}

// Return the resulting string
return result;
}


void decrypt(string text)
{
for (int i = 0; i < 28; i++)
{
cout << "Shift s: " << i << " decrypted string: " << decryptHelper(text, i) << endl;
}
}

// Driver program to test the above function
int main()
{
string text="attackatonce";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
string cipher = encrypt(text, s);
cout << "\nCipher: " << cipher;

cout << "Possible decryption: " << endl;
decrypt(cipher);

return 0;
}

Output:


Related Solutions

Chapter 11: Using the attached dataset “Chapter 11 Data Set 1” to test the research hypothesis...
Chapter 11: Using the attached dataset “Chapter 11 Data Set 1” to test the research hypothesis that males raise their hands in class more often than females. (Please note that these data are not exactly the data provided by the author). a. Is this a directional or non-directional hypothesis? b. Should you use a one-tailed or two-tailed test? c. What is the corresponding null hypothesis? d. Conduct the between groups t-test using Excel (either method). Use the .05 confidence level....
#11.     Consider the (fictitious) data labelled “problem # 11” which gives starting salaries of fresh...
#11.     Consider the (fictitious) data labelled “problem # 11” which gives starting salaries of fresh graduates and their CGPA at the time of graduation. (i) Construct a simple linear regression model that predicts the starting salary of a fresh graduate based on his/her CGPA. (a) clearly write the model, and (b) quote the R2 from Excel output and interpret the number. (10 + 5 =15 points) (ii) Using your model in (i) estimate the starting salary of a fresh...
Financial Management Chapter 11 Case CompU is looking at starting a major advertising campaign to further...
Financial Management Chapter 11 Case CompU is looking at starting a major advertising campaign to further expand their business. Based on preliminary estimates, the advertising campaign will cost $100,000 and will increase revenue by $80,000 per year for the next 5 years. The increased expenses excluding depreciation will be $35,000 per year. Inventory will increase by $25,000, accounts receivable will increase by $20,000, accounts payable will increase by $20,000, and accruals will increase by $15,000. The company is in the...
Starting Out with Java (6th Edition) chapter 8, 3PC Requirement: Each of the RoomCarpet and RoomDimension...
Starting Out with Java (6th Edition) chapter 8, 3PC Requirement: Each of the RoomCarpet and RoomDimension classes should have a toString method, an equals method, a copy method, and a copy constructor. The RoomCarpet class should have mutators and accessors for both of its fields.
“Starting Out with Python”, Chapter 5, introduces the student to functions. Describe in your own words...
“Starting Out with Python”, Chapter 5, introduces the student to functions. Describe in your own words some ways functions are beneficial to the programmer. In other words, why might a programmer want to use functions?
Using the below program as a starting point C++ Program - Arrays- Chapter 9     Include...
Using the below program as a starting point C++ Program - Arrays- Chapter 9     Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step so I can...
Chapter 8: Employees and Payroll Additionally, please refer to Chapter 11 in your Cengage Accounting eText,...
Chapter 8: Employees and Payroll Additionally, please refer to Chapter 11 in your Cengage Accounting eText, accessible from the eText link in the Course Navigation Panel to the left of your screen. Requirement Tonya Latirno is a staff accountant for Cannally and Kennedy, a local CPA firm. For the past 10 years, the firm has given employees a year-end bonus equal to two weeks' salary. On November 15, the firm's management team announced that there would be no annual bonus...
2. What is Chapter 7 liquidation and Chapter 11 reorganization? When should each be used? Please...
2. What is Chapter 7 liquidation and Chapter 11 reorganization? When should each be used? Please choose one company that has gone through either type of bankruptcy proceeding and describe the circumstances leading up to the filing.
Chapter 6, Starting out with Programming and Logic, 4th Edition, Page 265 #6 Kinetic Energy In...
Chapter 6, Starting out with Programming and Logic, 4th Edition, Page 265 #6 Kinetic Energy In physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object’s kinetic energy: KE=12mv2 The variables in the formula are as follows: KE is the kinetic energy, m is the object’s mass in kilograms, and v is the object’s velocity, in meters per second. Design a function named kineticEnergy that accepts...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT