You are on page 1of 4

PREV

⏮ 4.9. Finding Things i…

Topics

C++ Cookbook

N EXT

4.11. Removing a Su… ⏭

4.10. Finding the nth Instance of a Substring
Problem
Given two strings source and pattern, you want to find the nth occurrence of

pattern in source.

Solution
Use the find member function to locate successive instances of the substring you are looking for.
Example 4-17 contains a simple nthSubstr function.
Example 4-17. Locate the nth version of a substring
#include <string>
#include <iostream>
using namespace std;
int nthSubstr(int n, const string& s,
const string& p) {
string::size_type i = s.find(p);
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

// Find the first occurrence
pdfcrowd.com

By "overlap. the sea. the trees".com .find(p. the sky. cout << nthSubstr(5. j < n && i != string::npos. you can add a parameter to account for substrings that may or may not overlap with themselves. p) << '\n'. for (j = 1. } Discussion There are a couple of improvements you can make to nthSubstr as it is presented in Example 4-17. Second. p) << '\n'. as in the word "abracadabra. s. string p = "the". s. cout << nthSubstr(2. First." where the last four characters are the same as the first four. // Find the next occurrence if (j == n) return(i). Example 4-18 demonstrates this. s. p) << '\n'. open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd. } int main() { string s = "the wind. ++j) i = s." I mean that the beginning of the string matches part of the end of the same string.int j. else return(-1). cout << nthSubstr(1. you can make it generic by making it a function template instead of an ordinary function. i+1).

bool repeats = false) { string::size_type i = s. else return(-1).com .find(p.Example 4-18. template<typename T> int nthSubstrg(int n. const basic_string<T>& s. string::size_type adv = (repeats) ? 1 : p. int j. } int main() { string s = "AGATGCCATATATATACGATATCCTTA". j < n && i != basic_string<T>::npos.length(). open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.find(p). if (j == n) return(i). for (j = 1. string p = "ATAT". i+adv). An improved version of nthSubstr #include <string> #include <iostream> using namespace std. const basic_string<T>& p. ++j) i = s.

p. open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.000 other books and videos Choose a Username Your Email Address Choose a Password Start a Free 10-Day Trial Blog / Support / Feedback / Sign In Learn about Safari for Business © 2015 Safari. s.cout << << cout << << p << " as non-repeating occurs at " nthSubstrg(3. } The output for the strings in Example 4-18 is as follows: ATAT as non-repeating occurs at 18 ATAT as repeating occurs at 11 See Also Recipe 4. p << " as repeating occurs at " nthSubstrg(3.9 Unlock the rest of C++ Cookbook and 30. p) << '\n'.com . true) << '\n'. Terms of Service / Membership Agreement / Privacy Policy Have an account? Sign in. s.