DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Program on strings - Can anyone solve this program? (http://www.daniweb.com/forums/thread160368.html)

gharish Dec 2nd, 2008 1:56 am
Program on strings - Can anyone solve this program?
 
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

gharish Dec 2nd, 2008 1:59 am
Re: Program on strings - Can anyone solve this program?
 
can anyone do this for me....

Salem Dec 2nd, 2008 2:05 am
Re: Program on strings - Can anyone solve this program?
 
"Can" - yes, lots of people here "can"
"Will" - probably not, it's your homework, you make an effort which is more than an exerise in copy and paste of your assignment.

http://www.catb.org/~esr/faqs/smart-questions.html

babusek Dec 2nd, 2008 2:57 am
Re: Program on strings - Can anyone solve this program?
 
Go Through The Java Regular Expressions

gharish Dec 2nd, 2008 4:06 am
Re: Program on strings - Can anyone solve this program?
 
I tried it but i was unable to do it.

ithelp Dec 2nd, 2008 4:10 am
Re: Program on strings - Can anyone solve this program?
 
Quote:

Originally Posted by babusek (Post 748602)
Go Through The Java Regular Expressions

I guess he is looking for C++ code .

Luckychap Dec 2nd, 2008 4:37 am
Re: Program on strings - Can anyone solve this program?
 
Show us what you tried so far.

Salem Dec 2nd, 2008 12:52 pm
Re: Program on strings - Can anyone solve this program?
 
> 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).


All times are GMT -4. The time now is 12:38 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC