You would need to create a parsing algorithm that recognizes and "extracts" each word indevidually into a vector/array. From there you can search for an indevidual word (like vollyball), and have it display each word in brackets until it comes to the word. After that, the vector/array number that correlates with that word being searched for can be used to calculate how many words are inbetween the word being searched for, and the start of the sentence/file.
___________________________________________________________
To get you started on the algorithm:
Markers which speicfy a word:
space before OR after; space before OR a punctuation after.
we use 'OR', because after a word, there can be either a space, or punctuation.
treat the string like an array: STRING[x]
use the size() function: string.size() or vector.size() = the total number of characters/(strings, numbers etc...) in any given string/vector.
____________________________________________________________
Now, if I were you, i would write the algorithm into 1 function, and make that function use the address of the varaible arguments. ex:
|
void find_word(string& sentence, unsigned int& number_of_instances, unsigned int& number_of_separatingwords)
|
This will directly modify the variable argumants given to it by the function which called it. This technique is very usefull if you want to return more than 1 variable.
This is a lot of work, but it will prove to be extremely more reliable, as it can recognize what is a ward, and what isn't and sort them out. You can make it display the sentence, and then the data which is relevant to that sentence (such as the number of words.)