Question: Write a program called hw1d which can encode and decode ASCII text files, i.e., any file you can view and make sense of such as

Write a program called "hw1d" which can encode and decode ASCII text files, i.e., any file you can view and make sense of such as source code or an html file. Since the goal is not to prevent bad guys from reading your files, we take a simple approach based on systematic character shifting.

Computers represent alphanumeric characters using what is called an ASCII code which is nothing more than a a unique 8-bit number. For example, newline ' ' is 10 (0x0A), space ' ' is 32 (0x20), digit '0' is 48 (0x30), letter 'A' is 65 (0x41) while letter 'a' is 97 (0x61) and so forth. (the Unix command "man 7 ascii" will show you all the encodings). Since alphanumeric characters are integer numbers, we can manipulate them. For example, 'a'+1 produces 'b' and 'a'+2 produces 'c'. We will use this idea to change all letters the same way. To keep things interesting, we will make sure lower case letters remain lower case and upper case letters remain upper case. We will not change white space, digits, and other characters. In order to this, you need to determine whether a character represents a letter and if so, whether it is lower or upper case. Use the library functions "isalpha", "islower", and "isupper" to get that job done. More about these below. The tricky bit is to ensure the encoding stays within the realm of lower and upper case letters. For example, 'z'+1 should be mapped to 'a', 'y'+4 should be mapped to 'c', and so forth. When you decode, you likewise need to ensure that say 'a'-1 maps to 'z' and 'c'-4 maps to 'y'. This is not hard but you do need to be careful when you implement these cyclic mappings.

How will the program know whether to encode or decode and how will it know which number to add or subtract to each letter? You tell it using command line arguments. The first is either "-encode" or "-decode". The second is an integer in the range 0-9.

UNIX> ./hw1d -encode 4 You can't read this! Csy ger'x vieh xlmw! UNIX> ./hw1d -encode 4 | ./hw1d -decode 4 You can't read this! You can't read this! 

Your program must check that the correct number and type of command line arguments is given. If not, an error message should be printed to stderr and program execution stopped. The encoding and decoding should be carried out on a textline-by-textline basis using functions "encode(string&,int)" and "decode(string&,int)". The two arguments are a reference to a textline and the magic shift you will apply to the lower and upper case letters in it. The function "getline(istream&, string&)" can be used to read textlines one at a time.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!