-
Notifications
You must be signed in to change notification settings - Fork 0
/
28_strstr.c
40 lines (38 loc) · 924 Bytes
/
28_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
36
37
38
39
40
/* Copyright (C) 2016 Leonard Ding <dingxiaoyun88@gmail.com> */
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
int strStr(char* haystack, char* needle) {
if(haystack == NULL || needle == NULL){
return 0;
}
if(strlen(needle) > strlen(haystack)){
return -1;
}
if(strlen(haystack) == 0 || strlen(needle) == 0){
return 0;
}
int i, j;
i = 0;
bool flag;
while(i <= strlen(haystack) - strlen(needle)){
if(haystack[i] == needle[0]){
j = 1;
flag = true;
while(j < strlen(needle)){
if(haystack[i + j] != needle[j]){
flag = false;
break;
} else {
j++;
}
}
if(flag == true){
return i;
}
}
i++;
}
return -1;
}