我是靠谱客的博主 追寻中心,最近开发中收集的这篇文章主要介绍统计字符串数组中所有单词出现的次数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#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]="";
	Word word[MAX];
	
	while(ch[i]!='')
	{
		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--]='';
			j=0;
		}
		i++;
	} 
	printf("n");
	i=0;
	while(i<count)
	{
		printf("单词%s的个数为%dn",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;
}

最后

以上就是追寻中心为你收集整理的统计字符串数组中所有单词出现的次数的全部内容,希望文章能够帮你解决统计字符串数组中所有单词出现的次数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部