You've been hired by Encoding Egrets to write a C++
console application that encodes and decodes text. Use a validation
loop to prompt for and get from the user a string that is up to ten
characters in length. Create value function
encodedString that takes the string as input and
returns the equivalent encoded string. Start with an empty encoded
string. Use a for loop to encode each character of the input
string. Use the following algorithm to encode a character and
append the encoded character to the encoded string:
● Convert the character to an integer (cast to int).
● Add 17 to the integer value.
● Mod the integer value by 256 to insure its stays within the
character range 0-255.
● Convert the integer back to a character (cast back to char).
Create value function decodedString that takes
the encoded string as input and returns the equivalent decoded
string. It does the reverse of function
encodedString. The original string and decoded
string should be identical. Use formatted output manipulators
(setw, left/right) to print the following rows:
● Input string.
● The encoded string (some characters may be unprintable).
● The decoded string.
And columns:
● A left-justified label.
● A left-justified string.
Declare global constants for the maximum string length (14), the shift value (17), the character boundary (256), and the column widths.
The output should look like this:
Welcome to Encoding Egrets
--------------------------
Enter a string up to 14 characters in length to encode:
Snow in November!
Error: the string is longer than 14 characters.
Enter a string up to 14 characters in length to encode: Hello World!
String: Hello World!
Encoded string: Yv}}Ç1hÇâ}u2
Decoded string: Hello World!
End of Encoding Egrets
Do not use this sample input for the final run that is pasted below.
Program code to copy:-
#include <iostream>
#include <iomanip>
using namespace std;
//Declare global constants
const int LEN = 14;
const int SHIFT = 17;
const int BOUND = 256;
const int COL = 20;
//Function Prototypes
string encodedString(string str);
string decodedString(string str);
int main()
{
string str;
//Printing Heading
cout << "Welcome to Encoding Egrets" <<
endl;
cout << "--------------------------" <<
endl;
do
{
//Prompt and read string from
user
cout << "Enter a string up to
14 characters in length to encode: ";
getline(cin, str);
//Validating entered string
if(str.length()>14)
cout <<
"Error: the string is longer than 14 characters." <<
endl;
}while(str.length()>14);
//Calling function to encode string
string encode = encodedString(str);
//Calling function to decode string
string decode = decodedString(encode);
//Displaying output using formatted output
manipulators
cout << left << setw(COL) <<
"String:" << left << setw(COL) << str <<
endl;
cout << left << setw(COL) <<
"Encoded string:" << left << setw(COL) << encode
<<endl;
cout << left << setw(COL) <<
"Decoded string:" << left << setw(COL) << decode
<<endl;
cout << "End of Encoding Egrets"
<<endl;
}
//Function takes the string as input and returns the equivalent
encoded string.
string encodedString(string str)
{
//Creating empty string
string temp="";
int n;
for(int i=0; i<str.length(); i++)
{
//Convert the character to an
integer (cast to int).
n = (int)str.at(i);
//Add 17 to the integer
value.
n = n + 17;
//Mod the integer value by 256 to
insure its stays within the character range 0-255.
n = n % BOUND;
//Convert the integer back to a
character (cast back to char)
temp = temp + (char)n;
}
//returns the encoded string
return temp;
}
//Function takes the encoded string as input and returns the
equivalent decoded string.
string decodedString(string str)
{
//Creating empty string
string temp="";
int n;
for(int i=0; i<str.length(); i++)
{
//Convert the character to an
integer (cast to int).
n = (int)str.at(i);
//Subtract 17 to the integer
value.
n = n - 17;
//Mod the integer value by 256 to
insure its stays within the character range 0-255.
n = n % BOUND;
//Convert the integer back to a
character (cast back to char).
temp = temp + (char)n;
}
//returns the encoded string
return temp;
}
Screenshot of output:-
Get Answers For Free
Most questions answered within 1 hours.