#include <cstdio>
#include <cstring>
#include <cctype>
char* trim(char *str){
while (*str == ' '){
++str;
}
return str;
}
int main(){
int n = 0;
int len = 0;
int sum = 0;
char buf[200];
memset(buf, '\0', sizeof(buf));
fgets(buf, sizeof(buf), stdin);
char *tmp = buf;
while (*tmp != '\0'){
tmp = trim(tmp);
len = 0;
while (isalpha(*tmp++)){
++len;
}
sum += len;
++n;
}
printf("n=%d, avglen=%d\n", n, sum / n);
return 0;
}算法竞赛入门经典 习题3-2单词们的平均长度
最新推荐文章于 2023-02-10 22:47:39 发布
本文介绍了一个使用C语言实现的简单程序,该程序可以读取标准输入中的文本,并统计其中单词的数量及平均长度。通过自定义trim函数去除字符串前导空格,再遍历字符串判断字符类型来完成统计。
2877

被折叠的 条评论
为什么被折叠?



