统计字符串数组中所有单词出现的次数

#include <string.h>
#include <stdlib.h>
#include <iostream>
#define N 20
#define MAX 10000 

typedef struct 
{
	char word[N];
	int count; 
}Word;

int check(char ch)
{
	if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
	{
		return 1;
	}
	else return 0;
} 
void search(Word word[],char str[],int &count)
{
	int i=0;
	
	while(i<count)
	{
		if(strcmp(word[i].word,str)==0)
		{
			word[i].count++;
			break;
		}
		i++;
	}
	if(i==count)
	{
		strcpy(word[i].word,str);
		word[i].count=1;
		count++; 
	}
}

void word_count(char ch[])
{
	int i=0,j=0,count=0;
	char temp_ch[N]="\0";
	Word word[MAX];
	
	while(ch[i]!='\0')
	{
		if(check(ch[i])==1)
		{
			temp_ch[j++]=ch[i];
		} 
		else if(check(ch[i-1]))
		{
			printf("%s ",temp_ch);
		 	search(word,temp_ch,count);
			while(j>=0) temp_ch[j--]='\0';
			j=0;
		}
		i++;
	} 
	printf("\n");
	i=0;
	while(i<count)
	{
		printf("单词%s的个数为%d\n",word[i].word,word[i].count);
		i++;
	}
	
}
int main()
{
	char str[]="Up to now I have read a lot of books,for example,magazines,novels and storybooks and so on.Up to now I have read a lot of books,for example,magazines,novels and storybooks and so on.But one of the books that I like best is My Life Story.It was created(创作)by an American writer-Helen Keller(海伦·凯勒)in 1902.She was a blind(盲的),deaf(聋的)and dumb(哑的)person.In the book,she wrote that she had not been able to see,hear or speak since the age of one year and seven months.This unusual thing made her very sad.When she was seven years old,she knew Miss Sullivan(沙利文),her good teacher.Helen was getting happier every day.Then,Miss Sullivan helped her learn how to write English words.At first,Miss Sullivan wrote some words on Helen's hands with her own fingers again and again.Helen was a very diligent girl.Because of this,she tried as much as possible to remember words.After that,she wrote and published(出版bai)many famous works.My Life Story is one of them.";
	word_count(str);
 	
 	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值