编写一个程序要求用户输入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出。

bobo840226 2006-11-20 10:16:12
编写一个程序要求用户输入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出。例如,一次运行程序情况如下:
Please input a passage:
The topic of this assignment is about array, pointer and string. In particular, the goal of the assignment is to give you experience for dividing programs into modules and using the pointer for manipulation of string data.
Words begin with t: 7
Words begin with a: 6
Words begin with i: 4
Words begin with p: 4
Words begin with o: 3
Words begin with d: 2
Words begin with f: 2
Words begin with g: 2
Words begin with m: 2
Words begin with s: 2
Words begin with e: 1
Words begin with u: 1
Words begin with y: 1

Total words: 37

设计提示:
int main()
{
char passage[500];
int total, count[26], sort[26];
char *token;
int i, j;

for(i=0; i<26; i++)
count[i]=sort[i]=0;
total = 0;

printf("Please input a passage:\n");
gets(passage); //需编写

InitScanner(passage);//需编写
while (!AtEndOfLine()//需编写) {
token = GetNextToken();
if(isalpha(token[0])){
count[tolower(token[0])-'a']++;
total++;
}
}

for(i = 0; i<26; i++)
sort[i] = count[i];
SortIntegerArray(sort, 26);

for(i = 0; i<26; i++){
if(sort[i]!=0){
for(j=0; j<26; j++){
if(count[j]==sort[i]){
printf("Words begin with %c: %d\n", j+'a', sort[i]);
count[j] = 0;
}
}
}
}
printf("\nTotal words: %d\n", total);

return 0;
}
请各位兄弟帮忙填写完整,先谢谢了
...全文
1226 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2006-11-23
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
char s[256] = {0}, tmp[256], *p; /*输入内容不超过256字符数*/
int count[26]={0}, total=0;
int index=0, i;

printf("Please input a passage:\n");
gets(s);
while(index<strlen(s))
{
strcpy(tmp, s);
p = strtok(tmp+index, " ");
index += strlen(p)+1;

total++;
if( p[0]>='a') count[p[0]-'a']++;
else count[p[0]-'A']++;
}

for(i=0; i<26; i++)
if(count[i]>0)
printf("Words begin with %c: %d\n", 'a'+i, count[i]);
printf("\n\nTotal words: %d\n\n", total);
system("PAUSE");
return 0;
}

如果输入的 passage 多于256个字符,
修改 定义的数组 大小就可以了
bobo840226 2006-11-23
  • 打赏
  • 举报
回复
谢谢楼上的兄弟,不过我运行的结果好象不能得到答案,请帮忙再看一下,先谢了
chai2010 2006-11-22
  • 打赏
  • 举报
回复
输入文本后,回车换行,然后ctrl^D结束
chai2010 2006-11-22
  • 打赏
  • 举报
回复
刚刚写的:)


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#define NELEMS(x) ((sizeof(x)) / (sizeof((x)[0])))

typedef struct { int n, c; } PS;

int cmp(const void *p, const void *q)
{
// 比较字符的数目

int t1 = ((PS*)p)->n;
int t2 = ((PS*)q)->n;
int fg = (t1<t2) - (t1>t2);

if(fg) return fg;

// 如果字符数目相同, 则一字符排序

t1 = ((PS*)p)->c;
t2 = ((PS*)q)->c;
fg = (t1>t2) - (t1<t2);
return fg;
}

main()
{
PS ps[26];

// 初始化

for(int i = 0; i < NELEMS(ps); ++i)
{
ps[i].n = 0; // 单词数目
ps[i].c = i; // 开头字符
}

printf("Please input a passage:\n");

// 统计字符

char buf[1024];
while(!feof(stdin) && scanf("%s", buf) != EOF)
{
if(!isgraph(buf[0])) break;
ps[tolower(buf[0])-'a'].n++;
}

// 排序

qsort(ps, NELEMS(ps), sizeof(ps[0]), cmp);

// 输出结果

int cnt = 0;

for(int k = 0; k < NELEMS(ps); ++k)
{
if(ps[k].n <= 0) break;

printf("Words begin with %c: %d\n", ps[k].c+'a', ps[k].n);
cnt += ps[k].n;
}

printf("\nTotal words: %d\n", cnt);

return 0;
}
sunnysheng 2006-11-22
  • 打赏
  • 举报
回复
只要编写这三处吗?SortIntegerArray(sort, 26);?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧