单词的长度(word)

输入若干个单词,输出它们的平均长度。单词只包含大写字母和小写字母,用一个或多个空格隔开。
(提示:全球最长英语单词长达189819个字母。)
输入
一行,若干个单词的句子,单词只包含大写字母和小写字母,用一个或多个空格隔开。
输出
所有单词的平均长度,保留一位小数。
样例输入
John reed was a schoolboy of fourteen years old
样例输出
4.3

可以换位思考
计算整个字符串长度 计算字符串中空格个数
空格个数+1就是单词数
长度-空格数 / 单词数 记得去浮点型 就是结果
比如这个测试样例
长度47
空格数8
(47-8) / (8+1)
39 / 9
4.33333保留以为小数
4.3



//3-2单词的长度 
#include<stdio.h>
#include<ctype.h>
#define MAXN 1000
char a[MAXN];
 
int main()
{
    char ch;
    int i=0,j=0,m=0,n=0;
    while(1)
    {
        scanf("%c",&ch);
        if(ch=='\n')
        {
            break;
        }
        else
        a[i++]=ch;
    }
    
    for(j=0;j<=i;j++)
    {
        if(a[j]==' ' && isalpha(a[j+1]) )
            n++;
        if(isalpha(a[j]))
            m++;    
    }
    
    double t;
    printf("%.1lf\n",(double)m/(n+1));
    return 0; 
}

//3-2单词的长度 
#include<stdio.h>
#include<ctype.h>
#define MAXN 1000
char a[MAXN];
 
int main()
{
    char ch;
    int i=0,j=0,m=0,n=0;
    while(1)
    {
        scanf("%c",&ch);
        if(ch=='\n')
        {
            break;
        }
        else
        a[i++]=ch;
    }
    
    for(j=0;j<=i;j++)
    {
        if(a[j]==' ' && isalpha(a[j+1]) )
            n++;
        if(isalpha(a[j]))
            m++;    
    }
    
    double t;
    printf("Avarage length is %lf\n",(double)m/(n+1));
    return 0; 
} 
#include <stdio.h>
#include <ctype.h>

int main()
{
    int word = 0, word_len = 0;
    float total_len = 0;
    char ch = getchar();
    do
    {
        if (isalpha(ch))
        {
            do
            {
                word_len++;
                ch = getchar();
            } while (ch != EOF && isalpha(ch));
            word++;
            total_len += word_len;
            word_len = 0;
        }
        else
        {
            do
            {
                ch = getchar();
            } while (ch != EOF && !isalpha(ch));
        }
    } while (ch != EOF);
    printf("%.1f", (float)total_len / (float)word);
    return 0;
}
#include <iostream>
#include <string>

using namespace std;

bool isSeparator(char ch);  //判断一个字符是不是分隔符(空格、逗号,句号)
int cntChar(string str);    //计算句子中字母数的函数
int cntWord(string sentence);   //计算句子中单词个数的函数

int main()
{
    string sentence;
    getline(cin, sentence);
    int letterNum = cntChar(sentence);
    int wordNum = cntWord(sentence);
    printf("%.1lf",letterNum*1.0 / wordNum);
    return 0;
}

int cntWord(string sentence)
{
    int wordNum = 0;
    int len = sentence.length();
    int i = 0;
    while (true)
    {
        //如果是分隔符,就跳过
        if (isSeparator(sentence[i]))
        {
            while (i <= len-1 && isSeparator(sentence[i]))
                i++;
        }
        //如果不是分隔符,就说明遇到单词,wordNum++,然后逐步跳过该单词
        else if (!isSeparator(sentence[i]))
        {
            wordNum++;
            while (i <= len-1 && !isSeparator(sentence[i]))
                i++;
        }
        if (i > len-1)
            break;
    }
    return wordNum;
}

bool isSeparator(char ch)
{
    if (ch == ' ' || ch == ',' || ch == '.')
        return true;
    return false;
}

int cntChar(string str)
{
    int len = str.length();
    int cnt = 0;
    for (int i = 0; i < len; i++)
    {
        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
            cnt++;
    }
    return cnt;
}