c语言小案例:获取文章中单词总数及含有1个字母到10个字母的单词的数量
来源:互联网 发布:网易centos镜像站 编辑:程序博客网 时间:2024/06/01 08:17
1 字数统计(10分)
题目内容:
你的程序要读入一篇英文文章,然后统计其中的单词数来输出。需要统计的数据为:
总的单词数量;
含有1个字母到10个字母的单词的数量。
单词和单词的间隔是由以下标点符号形成的:空格、tab、回车换行、逗号、句号、问号、括号、双引号和冒号。
这些符号不能被计入单词的长度中。
因此,对于下面的句子:
“What you see is a very hefty response,” said CNN Aviation Correspondent Rene Marsh.
其中的单词为:
What you see is a very hefty response said CNN Aviation Correspondent Rene Marsh
共14个。注意虽然这里的response,”后面跟了空格,我们并不能保证所有的输入中都会有这样规矩的空格。
单引号(表示缩写的’)和连字号(-)视做单词的一部分,所以“he’s”是一个单词,长度为4;
而“F-16 fighter jets”是三个单词。为了你的程序的方便,输入数据中不会出现两个连续的连字号。
输入格式:
一篇英文文章。
如何知道输入结束了?
如果使用scanf,它会返回这次读到了几个变量的值,当这个值小于你要求读的变量的数量时,表示输入结束了;
如果使用getchar,它在输入结束时返回EOF。
输出格式:
十一个数字,依次是单词数量和含有1到10个字母的单词的数量,以空格分隔,最后不含空格。
输入样例:
F-16 fighter jets escorted two passenger planes into Atlanta’s Hartsfield-Jackson
International Airport on Saturday after a bomb threat made on Twitter was deemed
credible, according to military officials.
Southwest Airlines Flight 2492 and Delta Flight 1156 landed safely at the airport
and were searched by bomb disposal units, according to airline officials.
Nothing out of the ordinary was found, officials said.
Delta spokesman Morgan Durant said, “It was a Portland,
Oregon, to Atlanta, Georgia, flight. Delta is supporting the sweeping process
carried out by authorities.”
输出样例:
85 2 11 11 11 6 11 10 10 9 1
时间限制:500ms内存限制:32000kb
#include <stdio.h>#include <stdlib.h>#include <string.h>char *article = "Hartsfield-Jackson\n" "F-16 fighter jets " "escorted two passenger planes into Atlanta's Hartsfield-Jackson\n" " International Airport on Saturday after a bomb threat made on Twitter was deemed\n" " credible, according to military officials.\n" "Southwest Airlines Flight 2492 and Delta Flight 1156 landed safely at the airport\n" " and were searched by bomb disposal units, according to airline officials.\n" " Nothing out of the ordinary was found, officials said.\n" "Delta spokesman Morgan Durant said, \"It was a Portland,\n" " Oregon, to Atlanta, Georgia, flight. Delta is supporting the sweeping process\n" " carried out by authorities.";int main() {// printf("Hello, World!\n"); // char *s = (char*)malloc(sizeof(char) * 400); // scanf("%[^\n]", s); char *s = article;// printf("%s\n", s); // hello world , I am here; char *dst = (char *) malloc(sizeof(char) * 2000); // 统计每个单词的长度,用字符串表示,空格分隔 int *count = (int *) malloc(sizeof(int) * 200);// 将每个单词的长度存放到数组中 int *co = count; char num[3]; int i = 0; int wordsize = 0; // 单词总数 while (1) { if (s + i == NULL || *(s + i) == '\0') {// dst[ci] = (char) (i + ('1' - 1)); if (i != 0) { wordsize += 1; *count = i; sprintf(num, "%d", i); strcat(dst, num); strcat(dst, " "); } break; } int space = 0; if (' ' == *(s + i) || '\t' == *(s + i) || '\n' == *(s + i) || ',' == *(s + i) || '.' == *(s + i) || '(' == *(s + i) || ')' == *(s + i) || '\"' == *(s + i) || ':' == *(s + i)) { space = 1; if (i == 0) { // need drop s += 1; } else { // need memo// printf("len=%d\n", i); s = s + i; *count = i; count++; wordsize += 1; sprintf(num, "%d", i); strcat(dst, num); strcat(dst, " ");// s += i + 1; } } if (space) { i = 0; } else { i++; } }// printf("[%s]\n", dst);// printf("ci = %d\n", wordsize); // 1. 单词总数统计完成 int s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0, s7 = 0, s8 = 0, s9 = 0, s10 = 0; for (int j = 0; j < wordsize; ++j) { int t = co[j];// printf("%d,", t); if (co[j] == 1) { s1 += 1; } if (t == 2) { s2 += 1; } if (t == 3) { s3 += 1; } if (t == 4) { s4 += 1; } if (t == 5) { s5 += 1; } if (t == 6) { s6 += 1; } if (t == 7) { s7 += 1; } if (t == 8) { s8 += 1; } if (t == 9) { s9 += 1; } if (t == 10) { s10 += 1; } } int currentSize[] = {wordsize, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10};// printf("%s", "\n\n"); for (int j = 0; j < 11; ++j) { if(j!=10){ printf("%d ", currentSize[j]); } else{ printf("%d", currentSize[j]); } } // 85 2 11 11 11 6 11 10 10 9 1 free(dst); return 0;}
console:86 2 11 11 11 6 11 10 10 9 1Process finished with exit code 0
- c语言小案例:获取文章中单词总数及含有1个字母到10个字母的单词的数量
- 匹配字符串美工单词的第一个字母
- 对字符串的操作 记录字符串中单词的个数 然后把单词的第一个字母变成大写的
- C语言 已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。
- c语言计算给定句子中各个单词的字母个数
- 倒置英文字母中单词的字母顺序
- C语言来实现字符串反转(只有单词反转,单词里的字母不反转)
- C语言来实现字符串反转 只有单词顺序反转,单词里的字母不反转
- c: 统计文章的单词数量
- 26个字母 在单词中可以发什么…
- 对文章单词的字母个数的计算
- 【c语言】译密码。将字母变成其后的第4个字母,非字母不变
- 王爽《汇编语言》问题7.9 将datasg段中每个单词的前4个字母改为大写字母
- AC自动机应用 多模式匹配 多个单词在文章中出现的次数-C语言实现
- HOJ 12811 单词对比(很巧的处理方法,运用26个字母的个数)
- 如何使用javascript将一段文字中的每一个单词的第一个字母转换为大写
- 首字母变大写 --输入一个英文句子,将每个单词的第一个字母改成大写字母。
- C语言编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
- 欢迎使用CSDN-markdown编辑器
- ajaxfileupload上传文件
- 百度技术委员会主席吴华:小度机器人背后的技术
- 智能穿戴依旧火爆、家电智能白热化、教育机器人兴起,IFA 2017亮点回顾
- 2017年中国软件业务收入报告新鲜出炉,华为以2178亿元连续16年蝉联榜首
- c语言小案例:获取文章中单词总数及含有1个字母到10个字母的单词的数量
- 存储结构分四类:顺序存储、链接存储、索引存储 和 散列存储
- 国际会议中conference,symposium,workshop的区别
- [BZOJ]3439: Kpm的MC密码 trie树+主席树(线段树合并)
- 单页微信分享 php+html+js 混编最简版
- 晶振电路的PCB设计
- 坚果智能影院实体布局再下一城 肇庆旗舰店火热开业
- vivo X20再爆重要产品细节:全面屏搭配Fake Wake面部识别
- 又一波猛料!iPhone 8启动Face ID解锁 ,还玩起了3D动画表情