c 查找A字符串在B字符串中是否存在,计算出现的次数

时间:2021-11-19 03:07:38
主要是应用了头文件<string.h>中的strstr函数
char *
strstr(const char *s1, const char *s2);

查找是否存在:

 #include<stdio.h>
#include<string.h>
int main(void)
{
char str3[] = {"abcabcdababc"};
char str4[] = "abc",*b;
int count=;
int len = strlen(str4);
b = strstr(str3,str4);
if(b)
printf("find!\n");
else
printf("not find\n");
return ;
}

计算出现的个数:

 #include<stdio.h>
#include<string.h>
int main(void)
{
char str3[] = {"abcabcdababc"};
char str4[] = "abc",*b;
int count=;
int len = strlen(str4);
b = strstr(str3,str4);
while(b)
{
count++;
b = strstr(b+len,str4);
}
printf("%d\n",count); return ;
}