-
Notifications
You must be signed in to change notification settings - Fork 0
/
strStr.c
35 lines (34 loc) · 917 Bytes
/
strStr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/implement-strstr/
/*
* Implement strStr().
*
* Returns the index of the first occurrence of needle in haystack, or -1 if
* needle is not part of haystack.
*/
#include <stdio.h>
int strStr(char* haystack, char* needle) {
char *ph, *pn;
char *cur_hay=haystack;
int len1=strlen(haystack);
int len2=strlen(needle);
while(cur_hay - haystack <= len1 - len2){
ph = cur_hay;
pn = needle;
while(*ph != '\0' && *pn != '\0' && (*ph == *pn)){
ph++;
pn++;
}
if(*pn == '\0')
return cur_hay - haystack;
cur_hay++;
}
return -1;
}
int main(){
char hay[] = "iloveleetcode";
char needle1[] = "leet";
char needle2[] = "geek";
printf("%s, %s, %d\n", hay, needle1, strStr(hay, needle1));
printf("%s, %s, %d\n", hay, needle2, strStr(hay, needle2));
return 0;
}