编写函数int fun(char *s1,char *s2),在字符串s1中统计字符串s2出现的次数并返回。若s2在s1中未出现,则返回0。例如: 若输入的字符串s1为:abaaAabcaabbabc

it2023-02-06  47

#include<string.h> #include<stdio.h> int fun(char *s1,char *s2) { int i,j,n=0; for(i=0;i<=strlen(s1)-strlen(s2);i++){ for(j=0;j<strlen(s2);j++) if(s1[i+j]!=s2[j]) break; if(j==strlen(s2)) n++; } return n; } 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); }
最新回复(0)