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

4
1

                                    std::string parentstring = &quot;Hello Agnosticdev, I love Tutorials&quot;;
std::string substring = &quot;Agnosticdev&quot;;
auto index = parentstring.find(substring);

4 (1 Votes)
0
3.56
9
R.Moon 90 points

                                    string str,sub; // str is string to search, sub is the substring to search for

vector&lt;size_t&gt; 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);
}

3.56 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
find occurrences of a substring in a string c++ how to find a substring in a sting in c++ find substring in c++ Finding string in another string cpp find the substring cpp find string inside wstring C++ finding all the substrings of a string in c++ find all occurrences of a full string c++ c++ std::string find sub string find all the occurrences of a substring in a string c++ how to find occurrence of a substring in a string in c++ how to find the substring of a string in c++ find occurances of a charecter in a string cpp find occurances of a charecter in cpp find substr in c++ searching a substring in a string c++ number of occurrences of a substring in a string in cpp how to find all subsequences of a string in cpp c++ search for exact substring c++ find exact substring find all substrings of a string in cpp how to search for a substring in a string in c++ how to find substrings in a string in c++ matching substring in c++ c++ substring find search string for substring c++ search string for substrings c++ c++ string view find substring example find all occurrences of a substring in a string how to find a substr in string in cpp searching a substring in a string in c++ find substring in as tring c++ search for a particular substring in a string in c++ c++ string substring find cpp program to find substring in string find occurence of a string after a aprticular index c++ find occurence of character in string c++ find substring function in c++ find substring in string cpp function to find a substring c++ find total occurence itertive method using string c++ find the number of occurrences of a substring in a string cpp cpp string find all occurence occurences of substring c++ how to find number of occurrences of a substring in a string in c++ how to find a substring in c++ using stl substring find in c++ c++ search substring substring search function c++ how to find substrings of a string c++ cpp string find substring find substring c++ finding a substring in the string c++ c++ find substring in string string find and string substring c++ best way to find a substring c++ how to find a substring c++ how to find all subsequence of a string c++ how to find all substrings of string c++ find all substrings of a string c++ how to find one string in another string c++ find string in another string c++ substring search c++ find string in given string c++ search for part of string c++ find substring in string c++ using substr how to find substring from a string in c++ is substring in string cpp find a string inside a string c++ find the substring in stl find all substrings of a string cpp string.find(substring) c++ find substr c++ find substrings in a string cpp find substring in string c++ gfg find word in part of string c++ find a match in substring c++ c++ program to find a substring in the string check a substring is in a string in c++ c++ string find substring substring and find string in cpp how to find substring in c++ find substring of string c++ fuction to find a substing in a string in cpp how to find substring of a string in c++\ how to find substring of a string in c++ find occurrences of substring in string c++ c++ substr find string find substring c++ finding a substring in a string c++ how to match a substring in c++ find all instances of a substring cpp cpp find substring how to find substring in a sting in cpp how to find a string inside a string in c++ check for a substring in a string cpp how to find substring in string c++ how to find a substring in string in cpp stl find a string in a string c++ find substring in a string in cpp find substring from string cpp find substring cpp finding a substring in a string cpp how to find a particular substring in a string in c++ how to find substr in c++ function to find substring in a string in c++ find subsstring c++ finding substring in a string c++ finding substring in c++ how to find substring in a string c++ how to search a substring in a string in c++ c++ find all occurrences in string how to find substring in a string in c++ c++ hot to find a substring c++ stl to find a substring in the string C++ find ~ substrng C++ find substring find number of occurrences of substring in string c++ find in c++ for substr c++ finding string in another string how to find substring in a string in c++ usinf found std::string find sub strings stl function to find a substring in a string in c++ search for a substring in a string c++ find a substring in c++ find a substring in a string in c++ c++ substring search Number of occurrences of a substring in a string in C++ find a substring in string c++ how to find the number of occurrences of a substring in a string in c++ c++ function to find a substring in a string check substring in string c++ how to find substring in c++ program to find substring in a string in c++ find substring in c++ string c++ find substring .contains c++ string searching in a string c++ find in string position c++ find something in a string c++ c++ std string contains find element in string cpp find a char in c++ string check what a string is in c++ search a substring in a string c++ find occurrence of word in string c++ find method in c++ string how to search in string in c++ function to find a substring in a string in c++ string.find c++ string find c++ check if one string contains another c++ find substring in cpp string in find in c++ ftring in find in c+= find vs contains string c++. find method char c++ string stl c++ find function search in a string c++ bool find substring in a string c++ search substring in string c++ c++ search string for substring string includes c++ find sbstring in string c++ locate substring C++ string stl issubstring find word in string in c++ how to know if a string is in another string c++ find anagrams of a string c++ find substring in string c++ stl print all substrings c++ check if substring is in string c++ c++ get position of string in a string c++ count the number of occurrences of a word in string check for substring in string c++ all substrings of a string c++ c++ program to count occurrence of a given word in a text file get substring of char array c++ substring in c+ string contain in c++ get all substrings of a string c++ find character in string c++ find occurrence of character in string c++ c++ string find substring string in c++ all permutations of a string c++ find all the occurrences of a substring in a string C how to find a substring in a string in c++ from starting check substring c++ check if string is substring c++ print palindromic substring c++ locate string in string c++ search characters index in string in c++ c++ count all occurrences find substring c++ find string in string in c++ test for substring in string c++ how to find all occurrences a substring in a string c++ how to find all occurrences a substring in c++ string how to find all occurrences of an element in c++ string how to find all occurrences of an element in c++ c++ find first occurrence of a substring how to find if a string is subset of another string c++ check if string is substring of another c++ check if a text string contains a particular substring c++ searcg string in c++ how to find a substring in a string c++ how to find a string in another string in c++ string find method cpp find location of a substring in c++ find index of first occurrence of a substring in c++ check string in c++ check occurrences of character in string c++ generating all substrings of a given string c++ what if string.find() function dont get a substring find all occurrences of a character in a string c++ find a substring in a string c++ search char in string c++ c++ find() npos search character in string c++ find a string in another string c++ c++ find occurrences in string find a char exist in string in cpp find char in string c ++ use find method to find substring in c++ str.find return value find string in string c++ program to find out that a particular substring is in given string in c++ occurrence of a string in another string cpp c++ if substring in string count substring in string c++ stl count a substring in a string c++ find a character in a string c++ if string in string c++ c++ if string in string strings contains strings c++ c++ find substring in a string c++ find word in string find substring in the given string cpp stl function substring of string in c-++ how to find number of occurrences of a substring in a string in cpp how to look for a substring in a string cpp how to find a substring in a string in c++ find if substring in string c++ how to check if a character is in a string in cpp how to find all instances of a string in a stricng in c++ check if substring in string cpp search for character in string c++ first occurrence of string in another string c++ search substring c++ string functions c++ contains find a string in another string in cpp string.contains in c++ how to search for a particular substring in a string in c++ find substring in a string c++ check for word in string C++ find strings in string c++ finding substring in cpp search for a character in a substring c++ what does the .find do in c++ find in string c++ string function to find substring c++ find char in string find occurrence of a word in string c++ find in cpp to find all occurences c++ search for substring how to distinguish string inside &quot;&quot; and not in c++ find element in string c++ find all occurrences of a substring in a string cpp finding index of character in string c++ find string in string cpp substr find in cpp stl find the positions of given string in string in c++ find occurrence of substring in string c++ search for substring cpp finfing where a suctring is present in s tsrong in c++ first index of character in string c++ indexof character in string c++ is string.substr presnt in c++11 is substr presnt in c++11 contains string c++ find first occurance of string c++ c++ string not in find first substring occurrence c++ search if a sting is in a string c++ find position of the first occurrence of a character in a string cpp c++ find specific part of sptring finding string in a string substring occurences string stl c++ find substring how to search in a string in ++ string.find() contains in string c++ how to find char index in cpp string contains substring c++ find positon of a string in c++ cpp string contains find if substring is in string c++ find characters position in string c++ c++ check substring in string string find substring find first occurrence of character in string c++ find substring in string c++ std string find all occurrences c++ string find all occurrences c++ finding numbers occurrence of a substring in a string in c++ find all occurrences of all character in string c++ find all occurrences of character in string c++ occurence of one string in another string c++ find all in string find all substring occurences in string c++ find all occurences of str c++ find all string c++ find all occurrences of a substring c++ check for occurence of a substring in a string c++ how to find the index of occurance of string in a string c++ how to find the index of occurance of string in a string ++ find all occurences of substring cpp find all positions of a char in a string c++ find for multiple occuraces string c++ c++ string find all occurrence search the number of occurences of a substring in a string c++ find all occurrences of a substring in a string c++ print all substrings of a string using recursion
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source