Skip to content

Commit 653910d

Browse files
authored
Merge pull request #2262 from seinlin/28
Add 0028-find-the-index-of-the-first-occurrence-in-a-string.c
2 parents 4a2f991 + d8df54c commit 653910d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int strStr(char * haystack, char * needle){
2+
int h_size = strlen(haystack);
3+
int n_size = strlen(needle);
4+
int i, j;
5+
if (h_size < n_size) {
6+
return -1;
7+
}
8+
for (i = 0; i < h_size - n_size + 1; i++) {
9+
for (j = 0; j < n_size; j++) {
10+
if (haystack[i + j] != needle[j]) {
11+
break;
12+
}
13+
}
14+
if (j == n_size) {
15+
return i;
16+
}
17+
}
18+
return -1;
19+
}

0 commit comments

Comments
 (0)