Horje
find all occurrences of a substring in a string c++ Code Example
find all occurrences of a substring in a string c++
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string s("hello hello");
    int count = 0;
    size_t nPos = s.find("hello", 0); // first occurrence
    while(nPos != string::npos)
    {
        count++;
        nPos = s.find("hello", nPos + 1);
    }

    cout << count;
};
cpp string find all occurence
string str,sub; // str is string to search, sub is the substring to search for

vector<size_t> positions; // holds all the positions that sub occurs within str

size_t pos = str.find(sub, 0);
while(pos != string::npos)
{
    positions.push_back(pos);
    pos = str.find(sub,pos+1);
}
find substring in string c++
std::string parentstring = "Hello Agnosticdev, I love Tutorials";
std::string substring = "Agnosticdev";
auto index = parentstring.find(substring);




Cpp

Related
check file exist cpp Code Example check file exist cpp Code Example
clear buffer memory in c / c++ Code Example clear buffer memory in c / c++ Code Example
void value not ignored as it ought to be Code Example void value not ignored as it ought to be Code Example
print hello world c++ Code Example print hello world c++ Code Example
eosio parse string Code Example eosio parse string Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8