实现一个字符串中单词个数的统计,并按照单词字典序输出单词以及单词的出现个数。使用strsep

请你实现一个字符串中单词个数的统计,并按照单词字典序输出单词以及单词的出现个数。Hint:a.不考虑标点符号.b.如果有单词是大写将单词转换为小写统计c.句子中除了单词全部都是空格,没有其他特殊字符。
Input:”I love you do you love me”
Output:
do 1
I 1
love 2
me 1

you 2


#include
#include
#include
#include
#define MAX 100
struct WORD
{
	char str[40];
	int num;
}words[MAX];

//linux 下没有该函数,大写字母转为小写字母
char *myStrlwr( char *s)
{
	char *str=s;
	while(*str!='\0')
	{
		if(*str>='A' && *str <='Z')
			*str=*str+32;
		str++;
	}
	return s;
}

//统计字符串出现的次数
int statistics(char * source,struct  WORD words[])
{
	char *p[MAX];
	int in=0;
	while( (p[in]=strsep(&source, " ")) !=NULL)
	{
		in++;
	}
	int i,j;
	int count=0; //统计当前的单词数
	for(i=0; i < in ; i++)
	{
		for(j=0; j < count ;j++)
		{
			if( strcmp(p[i] , words[j].str)==0)
			{
				words[j].num++;
				break;
			}
		}
		if(j==count)
		{
			strncpy(words[count].str, p[i], 40);
			words[count].num++;
			count++;
		}
	}
	return count;
}
int cmp(const void *a,const void *b)
{
	return strcmp( (*(struct WORD*)a).str, (*(struct WORD *) b).str);
}

//对字符串按字典序排序
void sort(struct  WORD words[],int len)
{
	qsort(words,len,sizeof(struct  WORD),cmp);
}

//显示
void print(struct  WORD  words[],int len)
{
	int i;
	for(i=0;i


你可能感兴趣的:(C,Data,Struct)