1#include <string>
2#include <iostream>
3
4using namespace std;
5
6int main()
7{
8 string s("hello hello");
9 int count = 0;
10 size_t nPos = s.find("hello", 0); // first occurrence
11 while(nPos != string::npos)
12 {
13 count++;
14 nPos = s.find("hello", nPos + 1);
15 }
16
17 cout << count;
18};
1string str,sub; // str is string to search, sub is the substring to search for
2
3vector<size_t> positions; // holds all the positions that sub occurs within str
4
5size_t pos = str.find(sub, 0);
6while(pos != string::npos)
7{
8 positions.push_back(pos);
9 pos = str.find(sub,pos+1);
10}
11
1std::string parentstring = "Hello Agnosticdev, I love Tutorials";
2std::string substring = "Agnosticdev";
3auto index = parentstring.find(substring);