求任意字串在主字符串中出现的次数

szm_baba   发布于 2018年03月11日   阅读次数: 243   我想说两句

参考链接   

int getCount(char *str, char *sub, char *count, char sublen) {
	int ret = 0;
	if (str == NULL || sub == NULL || count == NULL) {
		printf("fun:getCount() err: str == NULL || sub == NULL || count == NULL: %d", ret);
		return -1;
	}
	char *mybuf = str;
	int strlen1 = 0;
	while (*mybuf != NULL) {
		mybuf = strstr(mybuf, sub);
		mybuf = mybuf + sublen;
		strlen1++;
	}
	*count = strlen1;
	return  ret;
}

int main()
{
	char buf1[1024];
	char sub[200];
	int ret = 0;
	int count = 0;
	int len1, len2;
	scanf("%s",buf1);
	scanf("%s",sub);
	len1 = strlen(buf1);
	len2 = strlen(sub);
	printf("p: %s \n", buf1);
	printf("sub: %s \n", sub);
	ret = getCount(buf1, sub, &len1, len2);
	if (ret != 0) {
		printf("fun getCount err: %d", ret);
		return ret;
	}
	printf("len1: %d \n", len1);
	system("pause");
	return ret;
}