Introduction
You are going to design a program which will print out each line of its input that contains a specified pattern or string of characters. For example, searching for the pattern "Humpty" in an input text file that contains the following:

In Sixteen Hundred and Forty-Eight
When England suffered the pains of state
The Roundheads lay siege to Colchester town
Where the King's men still fought for the crown
There One-Eyed Thompson stood on the wall
A gunner of deadliest aim of all
From St. Mary's Tower his cannon he fired
Humpty Dumpty was its name
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
All the king's horses and all the king's men
Couldn't put Humpty together again
Humpty Dumpty sat on a wall
Humpty Dumpty was two inches tall
He fell down and broke his shell
Poor little egg, I wish him well

should produce the following output on the screen:

Humpty Dumpty was its name
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
Couldn't put Humpty together again
Humpty Dumpty sat on a wall
Humpty Dumpty was two inches tall


The program is to incorporate some additional features. It will allow four optional command line arguments. One option will ensure that each line of output is preceded with its line number, and the second will allow the user to view all of the lines except those that match the pattern. The third option will ignore the case of the strings being matched. The fourth option available, will ensure that only a count of the number of text lines selected is displayed rather than the lines themselves.


A common convention with C++ programs is that an argument beginning with a minus sign introduces an optional flag or parameter.

Using

-n to request line numbers,
-i to ignore case when performing the pattern match, and
-x to represent the except option,
-c to represent the count option,
the command: lookfor -n -i -x humpty test.txt

should produce the output:

1: In Sixteen Hundred and Forty-Eight
2: When England suffered the pains of state
3: The Roundheads lay siege to Colchester town
4: Where the King's men still fought for the crown
5: There One-Eyed Thompson stood on the wall
6: A gunner of deadliest aim of all
7: From St. Mary's Tower his cannon he fired
11: All the king's horses and all the king's men
15: He fell down and broke his shell
16: Poor little egg, I wish him well

the command: lookfor -c Dumpty test.txt

should produce the output:
5


The following are all examples of valid command lines:

lookfor Herne book.txt
lookfor -c 30000 salaries.doc
lookfor -x "the last time it happened" hope.fie
lookfor -n strcpy program.cpp
lookfor -inx "Fred Bloggs" names.doc
lookfor -i -n -x "Fat Controller" trains.doc
lookfor -x -n 25000 salaries.doc
lookfor -xic humpty humpty.txt

> I tried it but i was unable to do it.
So post it then.

Then we can help you and the help will be targetted to what you actually need (not what you wanted with your first post).

This article has been dead for over six months. Start a new discussion instead.