Question

You've been hired by Encoding Egrets to write a C++ console application that encodes and decodes...

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.

Homework Answers

Answer #1

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:-

Know the answer?
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
Similar Questions
You've been hired by Text Turtles to write a C++ console application that determines the reading...
You've been hired by Text Turtles to write a C++ console application that determines the reading grade level of the text in a file. The text comes from two files: SampleText1.txt and SampleText2.txt. Sample text1= It has often been said there's so much to be read you never can cram all those words in your head. So the writer who breeds more words than he needs is making a chore for the reader who reads. That's why my belief is...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary value into decimal. Allow the user to type in as many numbers as they want (one at a time) and end the program when they type in “0”. Use Horner’s method (given in step 3, below) to convert from binary to decimal. Sample Run Binary to Decimal Conversion Enter a binary string: 1101 1101 = 13 decimal Enter a binary string: 10011001 10011001 =...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
I tried using the modulo to skip the chars but I doesnt work in some cases....
I tried using the modulo to skip the chars but I doesnt work in some cases. There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include #include // print...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...