某兼职网站项目需求,继续吐槽吃相难看
#include <stdio.h>
#include <string.h>
/*
str:源字符串
st:查询字符串
ete:是否逐字查询 eg. "12121"中查找'121"非逐字只能找到1次,逐字能找到2次。
*/
int match_sum(char *str, char *st, bool ete)
{
char *s = str;
char *t = st;
int len = strlen(t);
int sum = 0;
bool flag = true;
while (flag)
{
printf("s=%s,t=%s,sum=%d\n", s,t, sum);
char *ret;
ret = strstr(s, t);
if (ret != NULL)
{
sum++;
if (ete)
{
s = ret + 1;
}
else
{
s = ret + len;
}
}
else
{
flag = false;
}
}
return sum;
}
int main()
{
int nn=20;
int n=10;
char haystack[nn] = "12121";
char needle[n] = "121";
bool ete = 1;
int sum= match_sum(haystack, needle, ete);
printf("sum=%d\n", sum);
return (0);
}