CS 1320 (Principles of Algorithm Design I):
Homework #8

Assigned:
November 12, 1999.
Due:
November 19, 1999, at start of class.
Credit:
10 points.

Programming problems

Instructions:
Write a C++ program to solve each of the problems below. For each program, unless otherwise instructed, turn in: See the guidelines for programming assignments for a more detailed description. Remember that at least one other human will read your program, so try to make it clear and readable. See the guidelines for tips on what makes for readable code, and the sample programs for examples.

"Multi-grep"

For this problem, the objective is to write an extended version of the optional "grep" program of Homework #6. The original program searched a text file for a pattern. The extended version you are to write for this problem searches a text file for multiple patterns and prints every line that matches at least one of the patterns. For example, given a file containing the following lines
        1776
        hello, world
        21478756282
        goodbye, world
and two patterns of " world" and "2", all but the first line will be printed. Each line that has at least one pattern should be printed exactly once regardless of how many patterns match the line and how many times the patterns match.

Your program should obtain the patterns using the standard input. Choose an appropriate way for the user to signal "no more patterns" and document your choice in your program's header comments. Your program can obtain the filename either as a command-line argument (preferable) or by prompting the user. Again, indicate in the program's header comments which of these your program does.

You may assume that the user enters at most fifty patterns. Make no assumptions, however, about the file's length. It could be much larger than the computer's memory. Make no assumptions about the length of the patterns or the lines in the file. Patterns may contain spaces.

Hints and tips

The string class
You may find this class, described in the text in section 10.3, useful. Display 10.11 on p. 632 describes some useful functions this class provides. For the entire list of functions, see the Standard Template Library WWW pages. (You may find these pages most useful as a way of looking up the details of the functions mentioned in the textbook.)

To use this class in your program, include the line #include <string>.

The sample program play_with_strings illustrates some things one can do with strings.

Here are some functions you may find particularly useful. You can find examples of using both these functions in play_with_strings.

Note that one can create an array of strings in the same way one creates arrays of other types.

Command-line arguments
The sample program echo1perline illustrates how to obtain command-line arguments.

Extension for extra credit

For 10 extra-credit points, you may extend the above program to search multiple files. Your program can get the filenames from the command line (e.g., the user might type multi_grep file1 file2 file3 file4 to search for the patterns in four files) or by querying the user. Your program should first search the first file for all the patterns, then the second file, etc. As output, you should print both the lines containing at least one of the patterns and the filename in which each line occurs. For example, suppose thisfile contains the following lines
        1776
        hello, world
        21478756282
        goodbye, world
and thatfile contains the following lines
        here is some text
        the world is a big place
        1234
        that's all, folks!
If you run your program to search for patterns " world" and "2", it should print something like the following as output:
        thisfile:  hello, world
        thisfile:  21478756282
        thisfile:  goodbye, world
        thatfile:  the world is a big place
        thatfile:  1234