Substring search
C++ Algorithms
nested loops
Flowchart (ISO 5807)
Source code
int indexOf(string s, string p) {
int n = s.length();
int m = p.length();
for (int i = 0; i + m <= n; i++) {
int j = 0;
while (j < m && s[i + j] == p[j]) {
j++;
}
if (j == m) {
return i;
}
}
return -1;
}