Finding number of substrings in string

I'm trying to understand the basic logic of this loop, my book (jumping into c++) just got into methods and the stl at page 210 so bear with me please:

1
2
3
4
5
6
7
8
9
10
11
12
13
string input;
int i = 0;
int cat_appearances = 0;

getline(cin, input, '\n');

for(i = input.find("cat", 0); i != string::npos; i = input.find("cat", i))
{
    cat_appearances++;
    i++;  // Move past the last discovered instance to avoid finding same
          // string
}
cout<<cat_appearances;


My basic understanding is that the defining of int i in the first part of the loop as 'i = input.find ( "cat", 0 )' means that the find method will find the index of the "c" in the first instance of cat (be it 10, 30, whatever) and then increment it by 1 so that the next find starts at the "a" as to avoid finding the same cat substring?

An example being "the cat ran to the food bowl, then another cat came and attacked it. Another cat heard the scuffle joined in."

so it would start at the "t" (aka 0) and run find and that finds the index for the first "cat" instance which is 4, then cat_appearances increments to 1 and i becomes 4 which increments to 5; then it runs 'i = input.find("cat", i)' but at 5 and then finds "cat" at 43 so i becomes 43 then increments to 44 and cat_appearances to 2, and so on? my exact counting may be off but I'm more trying to get the point across

Thanks!!
Last edited on
You are correct, but the code isn't stepping past "cat", it's only stepping past 'c'. It really should be:
1
2
3
4
5
6
7
8
9
10
11
12
string input;
int i = 0;
int cat_appearances = 0;

getline(cin, input, '\n');

for(i = input.find("cat", 0); i != string::npos; i = input.find("cat", i))
{
    cat_appearances++;
    i += strlen("cat");  // Move past the last discovered instance
}
cout<<cat_appearances;
The "cat" doesn't care, but counting "tete" from "tetetete" would make a difference. In other words, some test suites reveal more than others.
You are correct, but the code isn't stepping past "cat", it's only stepping past 'c'. It really should be:....


The "cat" doesn't care, but counting "tete" from "tetetete" would make a difference. In other words, some test suites reveal more than others.


Ok. I see what is being said here. I'm glad to know I was seeing it correctly but I see where this could cause problems in certain other functions.

Thanks a lot for the guidance. Really!
Last edited on
Topic archived. No new replies allowed.