I would like to search a file for whether certain words are present and extract the parts of the sentence that contain the words; I would like to find words that are a particular part of speech, and extract the part of the sentence from the word before the previously mentioned word, to the word after the previously mentioned word. I want to put these sentence fragments into different arrays according to what word they start with, I.e. The word which starts the sentence fragment.

You're set yourself a really good programing task here; it's simple to get something working, but there are many improvements and advances to be made from there.

You need to think about how you would solve this on paper to start with. You've got a file, containing letters; you can read them in one at a time, or a line at at a time, and then you have wat is essentially a long list of letters and spaces. How would YOU go through that looking for a word?

Seriously, start like that, get it working. For example, I might do something like this to start:

Open File.
Read in first complete word.
Compare it to the search word.
If it's the same, do something (like cout << "found one!" << endl; ).
Read in next word, repeat until end of file.

You might notice that you miss some. What if the word has punctiation next to it? word and word.are different. What if you're looking for egg, and the input word is eggplant? These are cases you can think about at this stage and deal with here, at the simple stage.

Then, get bigger. You want to do this with sentences? Decide how you can know what a sentence is (presumably, everything between certain punctuation marks?) and read in complete sentences instead of individual words, and then search the sentence for the word. At this point, you'd start thinking about using the standard string library for its search functions, and you'd be looking at the file reading libraries to see how to read up to a certain point (i.e. one sentence at a time). So:

Open File.
Read in first complete sentance.
Search it for the keyword.
If it's the same, do something (like cout << "found one here: " << theInputSentence << endl; ).
Read in next sentence, repeat until end of file.

Keep going from there, getting more efficient and elegant and sophisticated. If you get stuck using library functions or something doesn't work how you expect or that sort of thing, come back and show us what you've got.

You've got the chance here to really have some fun doing this, so don't let anyone just give you a complete solution! Good luck.

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