943,879 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 761
  • C++ RSS
Dec 2nd, 2008
0

Program on strings - Can anyone solve this program?

Expand Post »
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
Last edited by gharish; Dec 2nd, 2008 at 1:58 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gharish is offline Offline
3 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Program on strings - Can anyone solve this program?

can anyone do this for me....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gharish is offline Offline
3 posts
since Dec 2008
Dec 2nd, 2008
1

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 2nd, 2008
0

Re: Program on strings - Can anyone solve this program?

Go Through The Java Regular Expressions
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Dec 2nd, 2008
0

Re: Program on strings - Can anyone solve this program?

I tried it but i was unable to do it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gharish is offline Offline
3 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Program on strings - Can anyone solve this program?

Click to Expand / Collapse  Quote originally posted by babusek ...
Go Through The Java Regular Expressions
I guess he is looking for C++ code .
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Dec 2nd, 2008
0

Re: Program on strings - Can anyone solve this program?

Show us what you tried so far.
Last edited by Luckychap; Dec 2nd, 2008 at 4:39 am.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Dec 2nd, 2008
0

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).
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How can I add a loop for this code
Next Thread in C++ Forum Timeline: Easy C++ Contest!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC