The simplest way that is when you need not to think whether your code is valid or not is to use standard algorithm std::count with std::istream_iterator. It will count quickly how many times a word is present in a sentence. The code you are trying to write does not count *words*. It counts coincidences with a word.
For example if you have a statement
"This is a test"
and will try to count how often word "is" is encountered then you will get result 2. However in this statement only one word "is" is present.:)
Depending on where you are on the C++ learning curve, it might be better to follow though you own approach. To see if you can get there.
The algorithm approach is the kind of approach you'd use in practice (i.e. when your coding to solve problem rather than coding to learn), but whether or not it is appropriate in this case does depend on the point of the exercise you are doing.
Are you already familiar with algorithms?
Also, if you know istringstream, there is a solution based on it.
And it gets that bit more complicated if there's punctuation about...
I know alot, but still have much much more to learn. I just learned about stringstream yesterday so i need to practice with it more. Im trying to learn all about strings and string maniopulation before moving on. I dont think i know about algorithims.
Ok so can you tell me how i would do this? i cant figure out how to get my progrm to work correctly it keeps saying there are 0 instances in the line and also i cant figure out how to get a string fail, i tried string::npos but it doesnt work.
Note that you need to use the second param to find. If the string contains at least one instance of the character sequence you are searching for, then searching from zero will always find the first match. So after a successful find, you've got to restart the search from just after your last match.
so how would that look vlad im a bit confused?
For example it can look the following way
1 2 3 4 5 6
std::string s( "This is a sequence of words where each word is enclosed in white spaces." );
std::istringstream is( s );
std::cout << std::count( std::istream_iterator<std::string>( is ),
std::istream_iterator<std::string>(), "is" )
The only thing you should do yourself is to include required headers.:)
int main ()
cout << "Enter a line of text" << endl;
cout << "What word do you want to find" << endl;
cin >> find;
size_t numberOfInstances = 0;
const string t(find);
string::size_type pos = 0;
while((pos = enterText.find(t, pos)) != string::npos)
pos += t.size();
if(numberOfInstances == 1)
cout << "There is " << numberOfInstances << " instance of the word " << find << endl;
elseif(numberOfInstances > 1)
cout << "There are " << numberOfInstances << " instances of the word " << find << endl;
Why do you need to declare the string as a constant? why not just a regular string? i understand that const is used so that whatever you declare as const will never change, but why do you need it? why would it ever change? What is size_type? can you explain whats going on in here a little bit?