Program requirements

This program should ask the user for two strings: the first is the name of a file to search, and the second is a string to search for. The program should then show each line of the named file that contains the given string, with its line number.

For example, suppose that file data.txt contains the following.

'Twas brillig, and the slithy toves
did gyre and gimbol in the wabe;
all mimsy were the borogoves
and the mome raths outgrabe.
Now you run the program. It interacts with the user. A sample interaction is as follows. Parts in black are written by the program and parts in red are written by the user.
What file shall I search? data.txt
What string shall I search for? gim
2. did gyre and gimbol in the wabe;
Here is another sample run.
What file shall I search? data.txt
What string shall I search for? and
1. 'Twas brillig, and the slithy toves
2. did gyre and gimbol in the wabe;
4. and the mome raths outgrabe.

How to write the program

Open gedit and type in a data file. Save it with name data.txt.

Open gedit again. Copy your definition of function indexOf from lab 5 question 2 into the editor. (Do not copy it by hand. Copy it to the clipboard and paste it.) Save it with name find.cmg. Make the program have form

  Package find

  Import "collect/string".

  ...Your material here...

  %Package

Now modify the program so that it does what is desired for this assignment. You will find the indexOf function useful.

You will also find the following useful.

Build a loop. Each time around the loop, read a line of text and check whether the search string occurs in that line. If it does, then show the line. You will need to keep a counter so that you know which line it is. Keep going until there is nothing left to read.

When the program works, paste it into the box below and submit it.