单词计数问题

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

typedef struct
{
	char a[20];
	int count;
}words;

int check(char c)
{
	if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
		return 1;
	else
		return 0;
}

int main()
{
	words w[100];
	char s[300],temp[10];
	int i=0,j=0,m=0,k;
	temp[j]='\0';
	gets(s);
	while(s[i-1]!='\0')
	{
		if(check(s[i])==1)
			temp[j++]=s[i++];
		else
		{
			temp[j]='\0';
			for(k=0;k<m;k++)
			{
				if(strcmp(w[k].a,temp)==0)
				{
					w[k].count++;
					break;
				}
			}
			if(k==m)
			{
				w[k].count=1;
				strcpy(w[k].a,temp);
				m++;
			}
			j=0;
			temp[j]='\0';
			i++;
		}
	}
	for(i=0;i<m;i++)
		printf("%d  %s\n",w[i].count,w[i].a);
	system("pause");
	return 0;
}

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

typedef struct
{
	char a[20];
	int count;
}words;
words w[100];


int main()
{
	int i=0,k;
	char s[20];
	while(1)
	{
		s[0]='\0';
		gets(s);
		if(s[0]=='0')
			break;
		for(k=0;k<i;k++)
		{
			if(strcmp(w[k].a,s)==0)
			{
				w[k].count++;
				break;
			}
		}
		if(k==i)
		{
			strcpy(w[k].a,s);
			w[k].count=1;
			i++;
		}
	}
	for(k=0;k<i;k++)
		printf("%d  %s\n",w[k].count,w[k].a);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值