Assignment 1 - Changing Strings

Changing Strings

Systematically changing occurrences of strings or patterns in files is common in everyday use of UNIX. Various tools (such as sed) are provided for this purpose.

Write change: Given two strings, change copies lines from the standard input to the standard output replacing all occurrences of the first string by the second. change is invoked by the command

change from to
and prints the lines in the standard input changing all occurrences of the string from to the string to. The strings from and to do not have to be the same length. For example, if the file foo contains
Now is the time
for all good men
to come the aid of their country.
the command
change the first <foo
prints the following to standard out:
Now is first time
for all good men
to come first aid of firstir country.
In addition to printing the transformed text on standard out, change should print the number of substitutions performed and the number of characters by which the input file grew or shrunk as diagnostic information to standard error. For the above input, the standard error output should be as follows:
3 substitutions
+6 characters
The to string is optional; if it is omitted, the effect is to delete occurrences of the from string. Thus, the command
change the <foo
prints
Now_is__time
for_all_good_men
to_come__aid_of_ir_country.
where underscores denote spaces. Note the two spaces between is and time in the first line and between come and aid in the last line.

The standard error output should be:

3 substitutions
-9 characters
There is a convention that lines of text are no longer than 80 characters. Your program must behave reasonably with longer lines, that is it must not crash. However, it may ignore characters in the 81st column and beyond. Also, make sure your program does something reasonable with the boundary cases, e.g., a null from string (change ""), empty input, etc. Briefly explain how you deal with boundary cases in your readme file. Note that the line length of your output may grow beyond 80 characters as a result of substitutions. Do not truncate it.

An executable solution is available at /u/cs217/Assignment1/change. This implementation is 59 lines of C and uses 7 different routines from the Standard C library.

Submission

This program is easy, but write it as if it were a `real' program. That is, create a directory that contains change.c, a readme file, and a Makefile. change.c is your program, Makefile holds the instructions for compiling and linking your program, and readme is a brief description of your program. readme should include the program's input, output, author, and modification history. You can edit a copy of the solution's Makefile for this assignment. It's available in /u/cs217/Assignment1/Makefile. Submit your program electronically with the command
/u/cs217/bin/submit 1 readme makefile change.c
You may use other computing facilities to develop your program, but the submitted version must compile with lcc -A and execute correctly on Arizona. To encourage good coding practices, you will lose points for any warning messages generated by lcc during the compilation of your program.

Due: submitted by 11:59pm, Mon. 2/12/2001.