CSCI 3510
Spring 2004
Programming Assignment 1

First version due: Fri. Jan 23, 11:59pm.
Second version due: Fri. Jan 30 11:59pm


What the program should do

This is a simple program that you should be able to do if you are well prepared for this course.

Write a program that takes two arguments on the command line. The first should be a string (call it str) and the second the name of a file. Your program should print every line of the given file that contains str as a substring. Each line must be numbered, starting with line 1. For example, if your executable program is called find, the command

  ./find fish myfile.txt
will print each line in file myfile.txt that contains "fish". The output might be as follows.
3. the terns eat fish that they catch
10. fishermen see the terns migrating
Note that the string being sought is not required to be a full word. It can be part of a word, for example.


Requirements

This program must be written according to the following list of requirements.

  1. This program must be written in two modules. One module, which must be called find.cpp, should contain the main program. The other module, which must be called search.cpp, should contain a function that takes four parameters: (1) an array pattern of characters telling the string to search for; (2) an integer indicating the logical size of pattern. (3) an array text of characters to search; and (4) an integer indicating the logical size of text; (The logical size is the number of characters in the string.) That function should produce a boolean result: true if the search string occurs in the array to search, and false if not. The heading for the search function must be

       bool occurs(char pattern[], int patternSize, char text[], int textSize)
    
    There must be a third file, search.h, that contains a prototype for the function that is exported by search.cpp. Both find.cpp and search.cpp must include search.h. File search.h must not contain prototypes or comments about any functions other than occurs. Module find.cpp must not include search.cpp.

  2. The main program must use the function from the other module to check a line of text. It cannot do the check directly itself.

  3. File search.h must have a clear and concise contract describing what your occurs function does. The contract must clearly describe the role of each parameter, and the meaning of the result. The contract must not describe irrelevant things, such as the method used, or other details of the function body.

    File main.cpp must contain a comment describing what the program does.

    If you write any helper functions, then each one must have a clear contract describing what it does.

  4. The standard library contains a function called strstr that checks to see whether one string is contained in another. For this assignment you may not use that function, or any similar function. Do you own search. You may use other library functions such as strlen.

  5. You must turn in a Makefile that will build your program. It should build an executable file called find. Your makefile must be called Makefile.

  6. Your program must write its output to the standard output (cout).

  7. Both modules must be instrumented with debug prints as follows. In the main program, provide an option to print each line of text as it is read. Then show the search string and whether the search string was found for that line. The search.cpp module must have an option to print the text and the pattern, and either the index where the string was found, or a report that it was not found.

    Every debug print must be clearly labeled by the name of the function that printed it, and what is being printed. Raw debug prints are unacceptable.

  8. The debug prints must be protected by #ifdef's of the following forms. In find.cpp:

      # ifdef DEBUGFIND
          ... (debug prints here)
      # endif
    
    In search.cpp:
      # ifdef DEBUGSEARCH
         ... (debug prints here)
      # endif
    
    so that they can be turned on by setting DEBUGFIND and DEBUGSEARCH and turned off by not setting them.

  9. Your program must not defined DEBUGFIND or DEBUGSEARCH. That is for me to do. But before turning in your program you should try defining DEBUGFIND and DEBUGSEARCH to see that things work. You can do that as follows. In find.cpp:

      #define DEBUGFIND
    
    In search.cpp:
      #define DEBUGSEARCH
    

  10. If the file to be searched does not exist, your program must print a message that the file does not exist, and immediatly stop.

  11. Your program must not use any global variables. All communication between functions must be by parameter passing.

  12. Your program must be well indented and readable.


Turning in the assignment

To turn in this program, log into one of the Unix computers in the lab. (You can log in remotely.) Ensure that your files are there. Change to the directory that contains those files. Then issue command

  ~karl/3510/bin/submit 1a find.cpp search.cpp search.h Makefile
to submit version 1, and
  ~karl/3510/bin/submit 1b find.cpp search.cpp search.h Makefile
to submit version 2. You should receive immediate feedback on your submission. If you have any trouble, please let me know.


Hints

The main program should perform a loop.

  linenumber = 1.
  Loop
    Read a line into an array.  Determine how many characters are in the line.
    If there are no characters in the line
      If the end of file was reached then exit the loop.
    else
      If the pattern occurs in the line then print the line, numbered by linenumber.
    Add 1 to linenumber
  End loop

The search function should also contain a loop

  k = 0
  While there are still enough characters left in the text array from index k to the end do
    If the pattern occurs in the text at index k
      return true
    Add 1 to k.
  End loop

You might find it useful to write a function to determine whether the pattern occurs at a given index in the text.

You can get the command line parameters as follows. The heading for the main function should be

   int main(int argc, char* argv[])
Then the pattern is argv[1] and the file to read is argv[2]. Each is an array of characters that ends on a 'null character' (a character whose code is 0). For example, to open the file, you can write
   ifstream inf(argv[2]);


Testing

Test your program. In particular, make sure that it works correctly when the pattern occurs in the first and last lines of the file. Make sure that it does something sensible when the file that it is supposed to search does not exist.


Frequently asked questions about the solution method


Frequently asked questions about the requirements