#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;
}
统计字符串数组中所有单词出现的次数
最新推荐文章于 2021-12-22 15:46:09 发布