Be sure you have read the assigned readings for classes through 3/28.
Do the following programming problems. You will end up with at least one code file per problem. 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 1120 homework 4''). 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.
Write a C program that, given two command-line arguments infile and outfile, encodes the text from infile using rot13 and writes the result to outfile. It should print an appropriate error message if called with fewer than two arguments, or if either file cannot be opened.
For example, if infile contains
Now is the time for all good persons to come to the aid of their party. Hello world! 1234 !@#$then outfile will contain the following
Abj vf gur gvzr sbe nyy tbbq crefbaf gb pbzr gb gur nvq bs gurve cnegl. Uryyb jbeyq! 1234 !@#$
Hints:
#include <string.h> /* for strchr */ /* encode one character */ int encode(int inchar) { char* alphabet_lc = "abcdefghijklmnopqrstuvwxyz"; char* alphabet_uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (strchr(alphabet_lc, inchar) != NULL) { /* encode inchar using alphabet_lc and return */ } else if (strchr(alphabet_uc, inchar) != NULL) { /* encode inchar using alphabet_uc and return*/ } else return inchar; }
Hello world! There is a German word for worldview that I have forgotten. World without end. Goodbye!
and you search for world, the first and second lines should print. (It's easiest to do the match in a case-sensitive way, so the third line does not match.)
The program should accept two command-line arguments, the first containing the text to search for and the second containing the name of the file to search. (For extra credit, make the program work more like grep, which allows searching any number of files, or none, in which case it searches for the specified text in standard input. If you do this, and more than one file is specified, print matching lines in a way that makes it clear which file they came from. grep does this by preceding each matching line with the name of the file it came from, e.g.,
input.txt: There is a German word for worldview that I have forgotten.
but you may think of some format you like better.)
For this problem, read the file a line at a time, but be as careful as you can about reading text -- it is very easy to read more characters than will fit into the character array you're putting them in. (I usually recommend using the library function fgets for reading text line by line.) If the input file contains lines that are too long, you can just print warning messages about them.
Hints:
fgets() puts a newline character in its output area if it was able to read the whole line; if the line was too long, it does not. If that happens, the sample program discards the rest of the line. You could do something similar.