Create a program in C to encode and decode a message input b


Create a program in C++ to encode and decode a message input by the user.
1. Let the user input a message which will consist of a string or an array of characters.
2. Save the original message in a variable called, original_message.
3. Create a function named encode to encode the message. The function will receive a pointer to the original message and return the string with the encoded message.
4. Create a variable named encoded_message to hold the return value from encode.
5. Create a function named decode to decode the message. The function will receive a pointer to the encoded message and return the string with the decoded message.
6. Create a variable named decoded_message to hold the return value from decode.
7. Test your program with the following strings:
\"Onomatopoeia\", \"Readability\", \"Abstraction & Polymorphism\" \"C++ is the Best!!\"
The output will look like the following, where the first # means the original message and the other two are the encoded and the decoded messages.
Enter a string: #######
Encoded string: #######
Decoded string: #######


Expected output for each of the values required:


\"Onomatopoeia\": encoding: \"Rprodvrrrhld\"
\"Readability\": encoding: \"Thdfddlnlva\"
\"OOP & Polymorphism\": encoding: \"RRR & Rrnaortrjluo\"
\"C++ is the Best!! \": encoding: \"E++ lu vjh Dhuv!! \"
\"2004 !%^&*() ? \": encoding: \"2004 !%^&*() ? \"


Notes: The encoding/decoding will be done the following way:
- Capital and lower case letters will be encoded the same way.
- Consonants input will be shifted two positions. (Ex: b?d, h?j, p?r, x?z, z?b)
- Vowels input will be shifted three positions (Ex: a?d, e?h, i?l, o?r, u?w)
- Numbers and other special characters including blanks will remain untouched.

Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

Note: The decode method will not work the way you expect it to be. This is because of the encryption technique. More than one character can have same encoded value, for example the char \'O\' is encoded to \'R\' (after shifting 3 places because its a vowel), also char \'P\' is encoded to \'R\' (after shifting 2 places because its a consonent). This makes decoding practically inaccurate. If there is any D,H,l,R or X in the encoded text, the decoded text will have the wrong value

#include<iostream>

using namespace std;

//method to encode a string

string encode(string* message){

                string encoded=\"\"; //string to store encoded message

                char newchar;

                //looping through all characters

                for(int i=0;i<message->length();i++){

                                //getting current character

                                char c=(*message)[i];

                                if(c>=\'A\' && c<=\'Z\'){

                                                //upper case character

                                                //checking if it is a vowel

                                                if(c==\'A\' || c==\'E\' || c==\'I\' || c==\'O\' || c==\'U\'){

                                                                newchar=c+3; //shifting 3 places

                                                }else{

                                                                //consonent, shifting 2 places

                                                                newchar=c+2;

                                                }

                                                //wrapping around, if the character go past Z

                                                if(newchar>\'Z\'){

                                                                newchar=\'A\'+(newchar-\'Z\');

                                                }

                                                encoded+=newchar;

                                }else if(c>=\'a\' && c<=\'z\'){

                                                //lower case

                                                //performing similar operations like above

                                                if(c==\'a\' || c==\'e\' || c==\'i\' || c==\'o\' || c==\'u\'){

                                                                newchar=c+3;

                                                }else{

                                                                newchar=c+2;

                                                }

                                                if(newchar>\'z\'){

                                                                newchar=\'a\'+(newchar-\'z\');

                                                }

                                                encoded+=newchar;

                                }else{

                                                //other character, remaining unchanged

                                                encoded+=c;

                                }

                }

                return encoded;

}

//method to decode the message

/*

Please note that this method will not work the way you expect.

This is because of the encryption technique. More than one character

can have same encoded value, for example the char \'O\' is encoded to \'R\'

(after shifting 3 places because its a vowel), also char \'P\' is encoded

to \'R\' (after shifting 2 places because its a consonent). This makes

decoding practically inaccurate. If there is any D,H,l,R or X in the

encoded text, the decoded text will have the wrong value

*/

string decode(string* message){

                string decoded=\"\";

                char newchar;

               

                for(int i=0;i<message->length();i++){

                                char c=(*message)[i];

                                //reversing everything the encode() method did

                                if(c>=\'A\' && c<=\'Z\'){

                                                if(c==\'D\' || c==\'H\' || c==\'L\' || c==\'R\' || c==\'X\'){

                                                                newchar=c-3;

                                                }else{

                                                                newchar=c-2;

                                                }

                                                if(newchar<\'A\'){

                                                                newchar=\'Z\'-\'A\'-newchar;

                                                }

                                                decoded+=newchar;

                                }else if(c>=\'a\' && c<=\'z\'){

                                                if(c==\'d\' || c==\'h\' || c==\'l\' || c==\'r\' || c==\'x\'){

                                                                newchar=c-3;

                                                }else{

                                                                newchar=c-2;

                                                }

                                                if(newchar<\'a\'){

                                                                newchar=\'z\'-\'a\'-newchar;

                                                }

                                                decoded+=newchar;

                                }else{

                                                decoded+=c;

                                }

                }

                return decoded;

}

int main(){

                string original_message, encoded_message,decoded_message;

                //getting input and testing encode,decode methods

                cout<<\"Enter a string: \";

                getline(cin,original_message);

                encoded_message=encode(&original_message);

                cout<<\"Encoded string: \"<<encoded_message<<endl;

                decoded_message=decode(&encoded_message);

                cout<<\"Decoded string: \"<<decoded_message<<endl;

                return 0;

}

/*OUTPUT*/

Enter a string: Onomatopoeia

Encoded string: Rprodvrrrhld

Decoded string: Onomatoooeia


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site