Create template to find the nth Instance of a Substring - C++ STL

C++ examples for STL:string

Description

Create template to find the nth Instance of a Substring

Demo Code

#include <string>
#include <iostream>

using namespace std;

template<typename T>
int nthSubstrg(int n, const basic_string<T>& s, const basic_string<T>& p, bool repeats = false) {
   string::size_type i = s.find(p);// ww  w.jav a 2s.  c  o  m
   string::size_type adv = (repeats) ? 1 : p.length();

   int j;
   for (j = 1; j < n && i != basic_string<T>::npos; ++j)
      i = s.find(p, i+adv);

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

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

   cout << p << " as non-repeating occurs at " << nthSubstrg(3, s, p) << '\n';
   cout << p << " as repeating occurs at " << nthSubstrg(3, s, p, true) << '\n';
}

Result


Related Tutorials