习题3-2,单词的长度

来源:互联网 发布:office for mac汉化包 编辑:程序博客网 时间:2024/05/22 04:46

输入若干个单词,输出它们的平均长度。单词只包含大写字母和小写字母,用一个或多个空格隔开。

这里把单词的最大长度定成了100,(应该没有什么单词长度超过一百吧...)
#include <iostream>#include <string>#include <ctype.h>using namespace std;const int MAXN=100;char word[MAXN];int main(int argc, char *argv[]){    system("color 0a");    long s=0;    int n=0;    while( scanf("%s" , word) )    {           s+=strlen(word);           n++;    }    printf("平均长度:%.2lf\n",s/(double)n);    system("pause");    return EXIT_SUCCESS; }