CS35: Assignment 19
CS35: Programming and Problem Solving
Ray Ontko
Department of Computer Science
Earlham College
Due Friday, 1999/11/19
Reading Assignment
Read sections 8.1 through 8.8 in Deitel and Deitel.
Optionally, read appendices B2 and B3 in Kernighan and Ritchie.
Programming Exercise
Modify the sample program replace.c to
replace not only the first occurrence of the search string, but
have it replace all occurrences of the search string in the
original file.
While you're at it, fix two problems inherent in the original
program.
- if the input line is too long for the string buffer in your program
(i.e., the last character in the buffer is not a newline '\n'),
print an error message and stop,
- if the replacement string is longer than the search string, your
program may overflow the string buffer.
Also, consider the effect on the behavior of your program if the
empty string ("") is given as the search string.
For example:
$ ./replace ab abab
abra cadabra
ababra cadababra
Abstract
Abstract
alabaster
alababaster
Hint: Instead of building a string which contains the substitutions,
have the program write out the left portion of the string and
the replacement string as it goes, and then repeat the search/replace
process on the rest of the string.
Copyright © 1999,
Ray Ontko
(rayo@ontko.com).