Chinaunix首页 | 论坛 | 博客
  • 博客访问: 916319
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-07-22 23:46:36

    题目是这样的:  从输入的一组字符里统计各种字符出现的个数和所占的比例:

#include
#include
#include

int main(void)
{
    int ch;
    int num_cntrl=0;
    int sum=0;
    int num_space = 0;
    int num_digit = 0;
    int num_lower = 0;
    int num_upper = 0;
    int num_other = 0;
    while ( ( ch = getchar() ) != '\n' )
    {
        if ( iscntrl(ch) )      {sum++; num_cntrl++;}
        else if ( isspace(ch) ) {sum++; num_space++;}
        else if ( isdigit(ch) ) {sum++; num_digit++;}
        else if ( islower(ch) ) {sum++; num_lower++;}
        else if ( isupper(ch) ) {sum++; num_upper++;}
        else { num_other++; sum++; }
        }

    printf("total input strigs : %d\n",sum);
    float sum_f = (float)sum;
    printf("cntrl string: %d , %.2f%%\n",num_cntrl,num_cntrl/sum_f*100);
    printf("space string: %d , %.2f%%\n",num_space,num_space/sum_f*100);
    printf("digit string: %d , %.2f%%\n",num_digit,num_digit/sum_f*100);
    printf("lower string: %d , %.2f%%\n",num_lower,num_lower/sum_f*100);
    printf("upper string: %d , %.2f%%\n",num_upper,num_upper/sum_f*100);
    printf("other string: %d , %.2f%%\n",num_other,num_other/sum_f*100);

    }

    今天看的要字符串,这个小程序主要是学习ctype.h里在几个函数在用法。
阅读(576) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~