天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

单词的长度

Posted on 2012-08-11 23:11 hoshelly 阅读(541) 评论(0)  编辑 收藏 引用 所属分类: Programming
输入若干个单词,输出它们的平均长度。单词只包括大写字母和小写字母,用一个空格隔开。

代码测试通过:

#include<stdio.h>
#include<string.h>
int main()
{
    char s[1000];
    char *p;
    p=&s[0];
    int len=0,tot=0,ws=0,per_len,i;
    gets(s);
    len=strlen(s);
    for(i=0;i<=len;i++)
    {
        if( *p >='a' && *p<='z' || *p >='A' && *p<='Z')
        {
            tot++;
            p++;
        }

        else if( *p == ' ' || *p =='\0')
        {
            ws++;
            p++;
        }
    }

    per_len=tot/ws;
    printf("%d\n",per_len);

    return 0;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理