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

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 file and store the encoded message in a new output file. The second action: decode an encoded message provided by the user in an input text file 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 read the input file in.txt from the same directory within which your executable is run, and generate the output file out.txt in that same directory. The output file will be the encoded or decoded message in the input file as specified by the user.

Before we specify how your program is to encode/decode a message, let's look again at a subtle aspect of ansi-c with regards to files and the newline character . "Newline" from the keyboard is your "Enter" key. The "Enter" key generates ascii code 10 or line feed (LF). However, when a "newline" is stored in a file, it consists of two ascii characters instead of one character: line feed (LF ascii code 10) and carriage return (CR ascii code 13). Surprised? When you printf(" ") or printf("%c",' ') to the screen, the ' ' is a single ascii character code 10. When you fprintf(fp," ") or fprintf(fp,"%c",' ') to a file, your operating system writes two bytes 10 and 13 to the file instead of one byte ascii code 10. This is a holdover from typewriter-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 your program is to encode/decode a message. A message to be encoded 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. Any other characters (uppercase, asterisk, dollar sign, etc) will be "skipped over" and not placed into the encoded file.

The ascii character codes for the lowercase alphabet characters, the number symbol characters 0-9 , the blank space character, the period, and the newline 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 (plus the CR 13 that is discarded) for the newline. Any other ascii characters in the input file are not considered as part of the message and will 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 a message, your program 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 the 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 the range33-71 using the inverse mapping and store the decoded message in the output file.

Project requirement: 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: don't include a separate case in the switch( ) function for each and every possible input/output char value; too many lines of code. Be smart. Lowercase characters have ascii char codes 97-122. For encoding, ascii char codes 97-122 are mapped to ascii char codes 33-58. That is, you subtract 64 from the ascii char code 97 to obtain the encoded char value 33. Therefore, use the switch( ) function with a single case for the lowercase characters. Use a single case for encoding the digits 0-9 also. Similarly, for decoding, you can also use a single case for decoding the lowercase characters and a single case for decoding the digits.

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

In order to test your program, use Microsoft text editor notepad.exe to create a text file to be encoded, and create an encoded text file to be decoded. Alternatively, write a short Ansi-C program to create a file to encode or decode. For example, if you encode the file

abc abc

the encoded file will be !"#G!"#G If you encode the file AaBbCc

aAbBcC the encoded file will still be !"#G!"#G because the capital letters will be ignored in the encoding process, and newlines are encoded as 'G' ascii code 71. Another example of a file to be encoded is

this is a file to be encoded.ABC notice how it is all lowercase letters, the numbers 0 through 9, and the period .

and we first encode this file and then decode the encoded file, the commas and ABC are not included in the encoded file. Therefore when we decode the encoded file we would obtain:

this is a file to be encoded. notice how it is all lowercase letters the numbers 0 through 9 and the period .

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!