统计一个词在文本中出现的次数

include <stdio.h>
#include <stdlib.h>

int countchar(char *str1, char *str2)
{
	int n = 0;
	int i = 0;
	while (*(str1 + i) != '\0')
	{
		int j = 0;
		if (*(str1 + i) == *(str2 + j))
		{
			i += 2;
			j += 2;
			if (*(str1 + i) == *(str2 + j))
				n++;
		}

		i++;
	}
	return n;
}
int main()
{
	char str1[2000], str2[2000];
	int n;
	printf("请输入一个文本:\n");
	gets(str1);
	printf("请输入一个词:\n");
	gets(str2);
	n = countchar(str1, str2);
	printf("这个词:%s 在这个文本:%s 中出现了%d次\n", str2, str1, n);
	system("pause");
	return EXIT_SUCCESS;
}

如果需要更长的文本则需要把那个字符串加长。

猜你喜欢

转载自blog.csdn.net/weixin_41883223/article/details/80148457
今日推荐