Be sure you have read chapter 7 and sections 1 through 4 of chapter 8.
Do the following programming problems. You will end up with at least one code file per problem. Every code file should begin with comments identifying you as the author and describing its purpose. It should be readable to human readers as well as to the compiler, so use consistent indentation and meaningful variable names. Feel free to cut and paste code from any of the sample programs on the course Web site.
Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu, with each file as an attachment. Please use a subject line that mentions the course number and the assignment (e.g., ``csci 1320 homework 6''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in. Remember also that I will usually deduct points if your programs do not compile without warnings using the flags -Wall and -pedantic.
Write a C program that uses this scheme
to encode text from input file hw6a-input.txt,
put the result in output file hw6a-output.txt,
and print to standard output the number of characters
processed (including the ones that were not changed).
For example, if hw6a-input.txt
contains the following
Now is the time for all good persons to come to the aid of their party. Hello world! 1234 !@#$then hw6a-output.txt will contain the following
Abj vf gur gvzr sbe nyy tbbq crefbaf gb pbzr gb gur nvq bs gurve cnegl. Uryyb jbeyq! 1234 !@#$and the program will print that 98 characters had been processed (including spaces, newline characters, etc.). The program should print a reasonable error message if it cannot open the input and/or output file.
Hints:
int encode(int input_char) { if (('a' <= input_char) && (input_char <= 'm')) { return input_char + 13; } else if (('n' <= input_char) && (input_char <= 'z')) { return input_char - 13; } /* add code for uppercase letters, other characters */ }
count for character 10 (not printable) is 2 count for character 32 ( ) is 21 count for character 33 (!) is 2 count for character 35 (#) is 1 count for character 36 ($) is 1 count for character 46 (.) is 1 count for character 49 (1) is 1 count for character 50 (2) is 1 count for character 51 (3) is 1 count for character 52 (4) is 1 count for character 64 (@) is 1 count for character 72 (H) is 1 count for character 78 (N) is 1 count for character 97 (a) is 3 count for character 99 (c) is 1 count for character 100 (d) is 3 count for character 101 (e) is 7 count for character 102 (f) is 2 count for character 103 (g) is 1 count for character 104 (h) is 3 count for character 105 (i) is 4 count for character 108 (l) is 5 count for character 109 (m) is 2 count for character 110 (n) is 1 count for character 111 (o) is 11 count for character 112 (p) is 2 count for character 114 (r) is 5 count for character 115 (s) is 3 count for character 116 (t) is 7 count for character 119 (w) is 2 count for character 121 (y) is 1
Hints:
Initially you will probably want to test your program with input from the keyboard, but when you get it working, try running it on a longer text file (which you might get from the Internet -- or you could use a file containing source code for one of your programs!). Remember that you can have your program get its input from a file rather than the keyboard using input redirection.