The text file has 5 of the word 'the' and it comes out 5 but i cannot seem to extract the 2 words before the word 'the' and 5 words after the word 'the'. other then that how can i make this code better, maybe using stringstream, but i do not know how to. I'm still new to C++, lead me in the right direction please.
I am trying to compare input word from user to a sentence in a text file and count how many of the word appear in the sentence then try to print out the sentence which has the word that was compared , tell me how to do or head me to the right source? thank you
read user word
while (text file has sentences)
{
if (sentence has word)
count how many times word appears
print sentence
}
print total count of words found
thank you, from your algorithm i have the general idea, but from the above code i noticed that if i search a word for example the word 'an' ..it will count the word 'an' in any word that has an 'an' like 'android', 'and', 'answer'..how can i prevent that??
You could add a space to the word when you search for it.
// input text
"an android and his dog an"
// user word
"an"
// instead search for
"an" + ' ' == "an "
p = line_from_file.find(user_word + ' ', p)
After that you must remember the special case when the word appears last in the line and is not followed by a space. (You could add a space to the input text as well.)
Edit: also if your input text contains symbols, such as ,.?!;: and tabs, you should first make a pass and replace all of them with simple space.