Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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;
};
Comment

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);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: uri online judge 3145 solution in c++ 
Cpp :: find max value in image c++ 
Cpp :: multiply image mat by value c++ 
Cpp :: Name one example of a “decider” program that you regularly encounter in real life. 
Cpp :: write a code that adds two number 
Cpp :: Plus (programming language) 
Cpp :: set platformio to C++14 
Cpp :: ue4 find component c++ 
Cpp :: C++ std::async wait is taking forever 
Cpp :: cuda kernel extern shared memory 
Cpp :: input pdf latex 
Cpp :: c++ loop programs 
Cpp :: how to change certain number from set c++ 
Cpp :: c++ unary minus overload 
Cpp :: shout sharkest 
Cpp :: qstring insert character 
Cpp :: n queens c++ 
Cpp :: create n threads cpp 
Cpp :: stack implementation using linked list in cpp 
Cpp :: c++ print every element in array 
Cpp :: iterate vector from end to begin 
Cpp :: structure and function c++ 
Cpp :: sort function from bigest to smallest c++ 
Cpp :: calling struct to a struct c++ 
Cpp :: how to check size of file in c++ 
Cpp :: prints out the elements in the array c++ 
Cpp :: Frequency of a substring in a string C++ 
Cpp :: c++ cin operator 
Cpp :: print all elements of vector c++ 
Cpp :: print each number of digit c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =