int find(string needle, string haystack) {
int i, j;
for (i=0; i<haystack.size(); i++) { // i is my index for caterpillar
for (j=0; j<needle.size(); j++) { // j is my index for pill
if (i+j < haystack.size ()) {
if (haystack[i+j] != needle[j]) {
break;
}
}
}
return i;
}
return -1;
}
int main(){
cout << find("pill", "caterpillar") << endl;
}
#include
using namespace std;
int find(string needle, string haystack) {
int i, j;
for (i=0; i<haystack.size(); i++) { // i is my index for caterpillar
for (j=0; j<needle.size(); j++) { // j is my index for pill
if (i+j < haystack.size ()) {
if (haystack[i+j] != needle[j]) {
break;
}
}
}
return i;
}
return -1;
}
int main(){
cout << find("pill", "caterpillar") << endl;
}