Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add 0028-find-the-index-of-the-first-occurrence-in-a-string.c
  • Loading branch information
seinlin committed Feb 16, 2023
commit d8df54c0b4e3aa6dcba0ebbf543283109d69d120
19 changes: 19 additions & 0 deletions c/0028-find-the-index-of-the-first-occurrence-in-a-string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int strStr(char * haystack, char * needle){
int h_size = strlen(haystack);
int n_size = strlen(needle);
int i, j;
if (h_size < n_size) {
return -1;
}
for (i = 0; i < h_size - n_size + 1; i++) {
for (j = 0; j < n_size; j++) {
if (haystack[i + j] != needle[j]) {
break;
}
}
if (j == n_size) {
return i;
}
}
return -1;
}