Count repetitions of a substring in a string

I have written the following code. I have to count repetitions of the substring in the string. What will be the code to count repetitions of the substring?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int t;
    cin>>t;
    cin.ignore();

    while(t--)
    {
        string str;
        getline(cin,str);

        size_t pos = str.find(' ');
        string sbstr = str.substr(pos+1);
    }

    return 0;
}


I've tried the following line for counting repetitions of the substring:
int count = count(str.begin(), str.end(), sbstr);

But compiler shows error: ‘count’ cannot be used as a function
Last edited on
You've defined count as an int. And no, you can't use an int as a function.

int count = std::count(str.begin(), str.end(), sbstr);, although that will also not work since the expected type of the third argument is char (and would require inclusion of the header <algorithm>.)

You should utilize one of these in a loop:
http://en.cppreference.com/w/cpp/string/basic_string/find
Topic archived. No new replies allowed.