Count number of occurrences of a given substring in a string

I am trying to count the number of times a given substring is present within a string. My could always gives 0, unless I have the same substring and string, which then it gives 1.

Help...
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str_to_search, str;
	int count = 0;

	cout << "Please enter the string to search: \n";
	getline(cin, str_to_search);
	cout << "Enter the substring to find: \n";
	getline(cin, str);

	int nPos = str.find(str_to_search, 0); // fist occurrence

	while (nPos != string::npos)
	{
		count++;
		nPos = str.find(str_to_search, nPos + 1);
	}

	cout << count << endl;
	system("Pause");
}
Last edited on
Think about it - you are seaching for str in str_to_search.

You have these the wrong way round in lines 15 and 20.
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 str_to_search, str;
	char str1[1000], str2[100];

	cout << "Please enter the string to search : ";
	str_to_search = (cin.getline(str1, sizeof(str1)), str1);

	cout << "Enter the substring to find : ";
	str = (cin.getline(str2, sizeof(str2)), str2);

	int count = 0;
	unsigned int nPos = 0;
	while((unsigned int)(nPos = str_to_search.find(str, nPos)) != (unsigned int)std::string::npos)
	{
		count++; nPos += str.size();
	}

	cout << "Found " << count << " substring(s) in the string" << endl;

	system("pause");
	return 0;
}
Actually, all he/she had to do was reverse the roles of str and str_to_search in the two lines 15 and 20! Why obfuscate that?
The problem is more than that.
1
2
3
4
5
6
7
int nPos = str.find(str_to_search, 0);

while (nPos != string::npos)
{
	count++;
	nPos = str.find(str_to_search, nPos + 1);
}


Should be :
1
2
3
4
5
6
7
int nPos = str_to_search.find(str, 0);

while (nPos != string::npos)
{
	count++;
	nPos = str_to_search.find(str, nPos + str.size());
}

This works for some reason, I just had to move int count = 0...
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()
{
	string str_to_search, str;
	cout << "Please enter the string to search: \n";
	getline(cin, str_to_search);
	cout << "Enter the substring to find: \n";
	getline(cin, str);
	int count = 0;
	int nPos = str.find(str_to_search, 0); 
	while (nPos != string::npos)
	{
		count++;
		nPos = str.find(str_to_search, nPos + 1);
	}

	cout << "There is a total of "<< count << endl;
	system("pause");
	return 0;
}
Last edited on
You should reverse the role of str_to_search and str.
1
2
3
4
5
6
int nPos = str.find(str_to_search, 0); 
while (nPos != string::npos)
{
	count++;
	nPos = str.find(str_to_search, nPos + 1);
}


Should be :
1
2
3
4
5
6
int nPos = str_to_search.find(str, 0); 
while (nPos != string::npos)
{
	count++;
	nPos = str_to_search.find(str, nPos + str.size());
}


Did you try my previous solution? It works.
Please enter the string to search : The boy is what is the look is
Enter the substring to find : is
Found 3 substring(s) in the string
Topic archived. No new replies allowed.