# include <stdio.h>
# include <string.h>
int strStr(char* haystack, char* needle)
{
int n = strlen(haystack);
int m = strlen(needle);
for (int i = 0; i + m <= n; i++)
{
bool flag=true;
for (int j = 0; j < m; j++)
{
if (haystack[i + j] != needle[j])
{
flag = false;
break;
}
}
if (flag)
{
return i;
}
}
return -1;
}
int main()
{
char haystack[100] = "mississippi";
char needle[10] = "issip";
int n = strStr(haystack, needle);
printf("%d\n", n);
return 0;
}
实现strStr函数,给两个字符串,请在第一个字符串中找出第二个字符串出现的第一个位置(下标从零开始),如果不存在,则返回-1。
最新推荐文章于 2024-05-28 14:35:23 发布