How to find all instances of a word in a string

Pages: 12
I have a program and im trying to find all instances of a word that the user typed in. but it just stops working when it gets to the while statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string textLine;
    string findText;
    int numberFound = 0;

    cout << "Enter a line of text" << endl;
    getline(cin, textLine);

    cout << "\n";

    cout << "What word do you want to find?" << endl;
    getline(cin, findText);

    while(true)
    {
        textLine.find(findText);
        numberFound += 1;
    }

    cout << numberFound << endl;
}
When will the while loop ever, ever end?
That's because the loop is infinite.

HTH,
Aceix.
ok well i put a break; in the loop but it only counts one, how do i count the others?
ok well i put a break;

Breaking the loop will let the loop break when it reaches the break; statement.
Sorry for the repetitions.

Aceix.
Also, how do you want it to cont the rest?
From the location it got the word? or the whole(length of the) sentence?

Thanks,
Aceix.
i want it to count how many instances of the word there are. the user types in a word they want to find and it should count how many times it finds them in the sentance,
So IF the word is found, add one to the total.

You're going to need an if.

When do you want the loop to stop? When the find fails.

So IF the word is not found, break the loop.
Ok i have the find part but how do i do the fail part? does string have a fail member like iostream does?

here is what i got in the loop

1
2
3
4
5
6
7
8
   if(textLine.find(findText))
        {
            numberFound += 1;
        }
        if()
        {
            break;
        }
You might need to use the second, optional parameter for the std::string's find() method?

From "std::string::find"
http://www.cplusplus.com/reference/string/string/find/

size_t find ( const string& str, size_t pos = 0 ) const;

pos - Position of the first character in the string to be taken into consideration for possible matches. A value of 0 means that the entire string is considered.

Andy
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.:)
Last edited on
so how would that look vlad im a bit confused?
@Ch1156

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...

Andy
Last edited on
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.
In that case, I'd set Vlad's suggestion to the side for now. You approach is more involved, but it is possible to extend your function to count words correctly.

Then later on you can give more sophisticated approaches a go. If you haven't looked at algorithms yet, I'd look at stringstream first.

If you can count all the "is"s in the string like the one Vlad proposed, then you can tweak you algorithm to only count whole words easily enough.

Then you can improve your algorithm!

Andy
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.
Well, what does string::find return?

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.

Andy
Last edited on
@Ch1156
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" )
	  << std::endl;


The only thing you should do yourself is to include required headers.:)


Last edited on
Compare results of the two approaches the previous one and this one


1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string s( "This is a sequence oo words where each word is enclosed in white spaces." );

size_t count = 0;

const std::string t( "is" );
std::string::size_type pos = 0;

while ( ( pos = s.find( t, pos ) ) != std::string::npos )
{
	count++;
	pos += t.size();
}

std::cout << count << std::endl;
Last edited on
Ok i modified it a bit to do what i needed, now can you explain some things in the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
    string enterText;
    string find;

    cout << "Enter a line of text" << endl;
    getline(cin, enterText);

    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)
    {
        numberOfInstances++;
        pos += t.size();
    }

    if(numberOfInstances == 1)
    {
        cout << "There is " << numberOfInstances << " instance of the word " << find << endl;
    }
    else if(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?

1
2
3
4
5
while((pos = enterText.find(t, pos)) != string::npos)
    {
        numberOfInstances++;
        pos += t.size();
    }
Last edited on
Pages: 12