偷了个懒,直接用strstr函数了,本质上双循环不用遍历全部可以得到结果。
int strStr(char * haystack, char * needle){
char *loc = NULL;
if ((NULL == haystack || '\0' == *haystack) &&
(NULL == needle || '\0' == *needle)) {
return 0;
}
return (NULL == (loc = strstr(haystack, needle))) ? -1 : (int)(loc - haystack);
}