Finding the nth Instance of a Substring - C++ STL

C++ examples for STL:string

Description

Finding the nth Instance of a Substring

Demo Code

#include <string>
#include <iostream>

using namespace std;

int nthSubstr(int n, const string& s, const string& p) {
   string::size_type i = s.find(p);     // Find the first occurrence

   int j;/*from  w  w  w . j av  a 2 s. c  om*/
   for (j = 1; j < n && i != string::npos; ++j)
      i = s.find(p, i+1); // Find the next occurrence

   if (j == n)
     return(i);
   else
     return(-1);
}

int main() {
   string s = "this is the test the test the test";
   string p = "the";

   cout << nthSubstr(1, s, p) << '\n';
   cout << nthSubstr(2, s, p) << '\n';
   cout << nthSubstr(5, s, p) << '\n';
}

Result


Related Tutorials