2.方法一:第一步我们先设置数组haystack,needle的值,然后将haystack,与needle的长度(int n = strlen(haystack), m = strlen(needle)),然后进行判断如果haystack[i]==needle[0],如果相等flage=1,然后haystack[i+v]!=needle[v](v<m),flage=0;,反之i++.
//方法一
int strStr(char* haystack, char* needle)
{
int n = strlen(haystack), m = strlen(needle);
int j = 0;
int flage= 0;
for (int i = 0; i < n; i++)
{
if (haystack[i] == needle[j])
{
flage = 1;
for (int v = 0; v < m; v++)
{
if (haystack[i + v] != needle[v])
{
flage = 0;
break;
}
}
}
if (flage)
{
return i;
}
}
return -1;
}
int main()
{
char haystack[20] = { "sadbutsad" };
char needle[10] = { "sad" };
int n= strStr(haystack, needle);
printf("%d ", n);
}