Skip to content

Instantly share code, notes, and snippets.

@ouwenshi
Created April 24, 2018 15:00
Show Gist options
  • Select an option

  • Save ouwenshi/b07bb112a5ab4130846388b263278aa7 to your computer and use it in GitHub Desktop.

Select an option

Save ouwenshi/b07bb112a5ab4130846388b263278aa7 to your computer and use it in GitHub Desktop.
Find a string S in a given given string.
#include <iostream>
using namespace std;
int findSubString(string s, string sub, const string &S, int pos, int cur) {
if (sub.empty())
return pos;
else if (s.empty())
return -1;
if (s[0] == sub[0])
return findSubString(s.substr(1), sub.substr(1), S, pos, cur + 1);
else
return findSubString(s.substr(1), S, S, cur + 1, cur + 1);
}
void main() {
string S = "myword";
cout << findSubString("hereis mywor dwow mywordhaha.", S, S, 0, 0) << endl;
//cout << findSubString("hereis mywor dwow mywo rdhaha.", S, S, 0, 0) << endl;
cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment