一道C语言的题目求解!!

编写函数int fun(char *s1,char *s2),在字符串s1中统计字符串s2出现的次数并返回。若s2在s1中未出现,则返回0。例如:
若输入的字符串s1为:abaaAabcaabbabca,字符串s2为:ab ,则程序输出:n=4
若输入的字符串s1为:abaaAabcaabbabca,字符串s2为:abd,则程序输出:No find
#include
#include
int fun(char *s1,char *s2)
{
}
int main()
{ char a[80],b[80];
  int n;
  printf("Please input the first string:");
  gets(a);
  printf("Please input the second string:");
  gets(b);
  n=fun(a,b);
  if(n==0)
    printf("No find\n");
  else printf("n=%d\n",n);
  return 0;
}

5个回答

忘贴图了图片说明

#include
#include
int fun(char *s1,char *s2)
{for(int i=0,i<=s1.length;i++)
int q=i;
{
for(int j=0;j<=s2.length;j++)
{ if(s1[i]=s2[j])
{
i++;
}
else i=q+1;
break;
}
}

}
int main()
{ char a[80],b[80];
int n;
printf("Please input the first string:");
gets(a);
printf("Please input the second string:");
gets(b);
n=fun(a,b);
if(n==0)
printf("No find\n");
else printf("n=%d\n",n);
return 0;
}

萌萌哒!

            #include<stdio.h>
        #include<string.h>
        int fun(char *s1, char *s2)
        {
            int strlenth(char *);
            //int alen = strlenth(s1);先弄错了,自己构造的函数
            //int blen = strlenth(s2);
            int alen = strlen(s1);
            int blen = strlen(s2);
            int n = 0;
            for (int i = 0; i<alen - blen+1; i++)
            {
                int f = 0;
                for (int j = i, k = 0; k<blen; k++, j++)
                    if (s1[j]== s2[k] )
                    {
                        f++;
                    };
                if (f == blen)   n++;
            }
            return n;
        }
        /*int strlenth(char *q)
        {
            int i = 0,sum;
            while(q[i] != '\0')
            {
                i++;
                sum = i;
            }
            //printf("长度:%d", sum);
            return sum;
        }*/
        int main()
        {
            char a[80], b[80];
            int n;
            printf("Please input the first string:");
            gets_s(a);
            printf("Please input the second string:");
            gets_s(b);
            n = fun(a, b);
            if (n == 0)
                printf("No find\n");
            else printf("n=%d\n", n);
            return 0;
        }

`

int fun(char *s1, char *s2)
{
    int sum = 0;
    for (int i = 0; i < (int)strlen(s1); i++) {
        int p = i;
        int q = 0;
        while (q<(int)strlen(s2) &&s1[p] == s2[q]) {
            p++;
            q++;
        }
        if (q == strlen(s2))sum += 1;
        if (q > 0) {
            i = i + q - 1;
        }
    }
    return sum;
}

简单添了一些注释

 int fun(char *s1, char *s2)
{
    int sum = 0;//用于统计s1中s2出现的总次数
    for (int i = 0; i < (int)strlen(s1); i++) {//依次遍历s1
        int q = 0;//s1中的每个字符均与s2的首字符比较,故每次比较前将s2的index置为0
        while (q<(int)strlen(s2) &&s1[i] == s2[q]) {
            i++;
            q++;
        }
        if (q == strlen(s2))sum += 1;
        if (q > 0) {
            i = i - 1;//这里希望将i调到当前位置,但是for循环会自动加1,故先减去1
        }
    }
    return sum;
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!