2020.9.21
leetode-初级算法-字符串-实现 strStr()
代码
int strStr(char* haystack, char* needle) {
int i, j;
int lenh = strlen(haystack);
int lenn = strlen(needle);
if (lenn == 0) {
printf("%d", 0);
return 0;
}
for (i = 0; i <= lenh - lenn; i++) {
if (haystack[i] == needle[0]) {
for (j = 0; j < lenn; j++) {
if (haystack[i + j] != needle[j]) {
break;
}
if (j == lenn - 1) {
printf("%d", i);
return i;
}
}
}
}
printf("%d", -1);
return -1;
}
执行结果