| | |
Program on strings - Can anyone solve this program?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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.
"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
"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
![]() |
Similar Threads
- hangman revised (VB.NET)
- Coding Question (C++)
- Hijackthis log RE: Trojan.Abwiz.F virus (Viruses, Spyware and other Nasties)
- Trojan Horse Startpage.MF (Viruses, Spyware and other Nasties)
- hi i have a question!!!!!! (^o^) (C++)
- How to improve program with string compairing? (C++)
- I've got a nasti VX2 infection (Viruses, Spyware and other Nasties)
- Counting the amount of letters in a phrase! (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: How can I add a loop for this code
- Next Thread: Easy C++ Contest!
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






