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
Now is first time
for all good men
to come first aid of firstir country.
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.

You may assume that most lines are no longer than 120 characters, but your program must behave `reasonably' with longer lines. Also, make sure your program does something reasonable with the `boundary' cases, e.g., a null from string (change "") or empty input. Briefly explain how you deal with these cases in your readme file.

An executable version of Dave Hanson's solution is available in /u/cs217/1/change. His implementation takes 29 lines and uses six 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/1/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 and execute correctly on Arizona.

Due: submitted by 11:59pm, Mon. 2/8/1999.