<<

ECE 3331, Summer-3, HW 3, Due 6/16

Problem 1. Chapter 3.3, Exercises 3, 5, and 11.

Problem 2. Section 3.4, Exercise 1.

Problem 3. Section 3.5, Exercise1.

Problem 4. Section 3.6, Exercise 3.

Problem 5. Section 3.7, Exercises 6.

Problem 6. Section 3.7, Exercises 7.

Problem 7. Due 6/19 at 11:59:00 pm on Blackboard.

You will write a program that can perform two actions for a user.

The first action: encode a text message provided by the user in an input text and store the encoded message in a new output file. The second action: decode an encoded message provided by the user in an input and store the decoded message in an output text file.

Your program will begin by prompting the user whether they want to encode a messsage in a file or to decode a message in a file. To do this, your program will ask the user to enter an 'e from the keyboard for "encode", or a 'd' from the keyboard for decode. If the user enters any other character, your program repeats the prompt : indefinitely.

Next your program will prompt the user for the name of the input file and the name for an output file. Those files are to be stored in the same directory as your executable code, so no pathname is required.

Before we specify how to encode/decode a message, let's look at a subtle aspect of ansi- with regards to files and the character . "Newline" from the keyboard is your "Enter" key. The "Enter" key generates code 10 or line feed (LF). However, "newline" in a file consists of two ascii characters instead of one character: line feed (LF ascii code 10) and (CR ascii code 13). Surprised? When you ("\n") or printf("%c",'\n') the '\n' is a single ascii character code 10. When you fprintf(fp,"\n") or fprintf(fp,"%c",'\n') to a file, your writes two bytes 10 and 13 to the file instead of one byte ascii code 10. This is a holdover from -style printers. When you execute fscanf(fp,"%c",&cc) to read-in a character from a file and the next two bytes in that file are 10 and 13, your operating system stores a single byte 10 into the char variable cc and discards the asci code 13. Again, this is a holdover from typewriter-style printers.

Now let's specify how to encode/decode a message. A message is defined as consisting of only the lowercase alphabet characters, the number symbol characters 0-9 , the blank space character, the period, and the newline.

The ascii character codes for these characters are decimal 97-122 for the lowercase letters, 48-57 for digits 0-9, 32 for the blank space, 46 for the period, and 10 for the newline. Any other ascii characters in the input file are not considered as part of the message and should not be encoded into the output encoded file. Therefore there are a total of 26+10+3=39 allowable characters (a-z, 0-9, blank, period, LF).

To encode, you will map these 39 ascii characters to ascii codes 33-71 (inclusive of 33 and 71) in the output file. That is, 'a' is encoded as '!', 'b' is encoded as '"' (the double quotes), 'c' is encoded as '#', etc. That is, the lowercase alphabet a-z is encoded to ascii character codes 33-58. The digit characters 0-9 are encoded to ascii character codes 59-68, and blank, period, and LF are encoded to ascii character codes 69, 70, and 71 respectively.

To decode a file, you will ignore any characters in the input file that are not decimal 33- 71. You will decode characters in this range using the inverse mapping and store the decoded message in the output file.

Use the switch( ) statement to determine the ascii character code to print to the file when you encode or decode each character in the input file.

Note, there can be disallowed characters (for example uppercase letters) in a file to be encoded as well as in a file to be decoded. Your encoder/decoder should skip over these disallowed characters.