现在的位置: 首页 > 综合 > 正文

一道题_20121216

2017年12月11日 ⁄ 综合 ⁄ 共 724字 ⁄ 字号 评论关闭

开始写写博客,做做笔记,学习学习。

输入一段英文,统计其中每个英文单词出现的次数,并输出,输出形式如下:

输入 the number is the number
结果: the   number   is
            2      2             1


#include <stdio.h>
#include <string.h>

int main()
{
	char ch = '\0';
	char str[100] = {'\0'};
	int i = 0;
	printf("输入一段英文:");
	while((ch = getchar()) != '\n')
	{
		str[i] = ch;
		i ++;
	}
	char *str1[20] = {'\0'};
	i = 0;
	str1[0] = strtok(str, " ");

	while(str1[i] != NULL)
	{
		i++;
		str1[i] = strtok(NULL, " ");	
	}

	int szCalaute[100] = {0};
	int temp = 0, it = 0;
	for(int m = 0; m < i; m++)
	{
		int k =1;
		for(int n = 0; n < i; n++)
		{
			if((strcmp(str1[m], str1[n]) == 0) && m != n)
			{	
				//确定第1个与之相同单词的位置,用temp保存
				if(k == 1) temp = n;
				k++;				
			}
		}	
		//相同单词出现次数大于2的情况
		if(k >= 2 && m > temp) continue;		
		printf("%s\t", str1[m]);
		szCalaute[it] = k;
		it++;
	}
	printf("\n");
	temp = 0;
	while(temp != it)
	{
		printf("%d\t", szCalaute[temp]);
		temp++;
	}
	printf("\n");
	return 0;
}

运行结果如下:


抱歉!评论已关闭.