代码如下所示:
int fun(char* s1, char* s2) { int i=0, j, k,count = 0; while(s1[i])//对s1进行遍历 { for (j = i, k = 0; s2[k] == s1[j]; j++, k++)//每遍历一次s2,k的值会被初始化为0 { //相当于s2会反复和s1的子字符串进行判断是否相等 if (s2[k + 1] == '\0')//判断s2是否结束 { count++; break; } } i++; } return count; } int main() { char s1[80], s2[80]; int count; gets(s1); gets(s2); count = fun(&s1[0], &s2[0]);//传递数组首地址和数组名均可 printf("%d\n", count); }
输出结果如下: