Exercises
See Appendix A for answers to the following exercises:
[7] Make a pattern that will match three consecutive copies of whatever is currently contained in
$what. That is, if$whatisfred, your pattern should matchfredfredfred. If$whatisfred|barney, your pattern should matchfredfredbarneyorbarneyfredfredorbarneybarneybarneyor many other variations. (Hint: you should set$whatat the top of the pattern test program with a statement likemy $what = 'fred|barney';.)[12] Write a program that makes a modified copy of a text file. In the copy, every string
Fred(case-insensitive) should be replaced withLarry. (So,Manfred Mannshould becomeManLarry Mann.) The input filename should be given on the command line (don’t ask the user!), and the output filename should be the corresponding filename ending with.out.[8] Modify the previous program to change every
FredtoWilmaand everyWilmatoFred. Now input likefred&wilmashould look likeWilma&Fredin the output.[10] Extra credit exercise: write a program to add a copyright line to all of your exercise answers so far, by placing a line like:
## Copyright (C) 20XX by Yours Truly
in the file immediately after the “shebang” line. You should edit the files “in place,” keeping a backup. Presume that the program will be invoked with the filenames to edit already on the command line.
[15] Extra extra credit exercise: modify the previous program so that it doesn’t edit the files that already contain the copyright line. (Hint: you ...