Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2529364
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-05 13:57:47

    有一篇文章,共有三行文字,每行有80个字符。要求分别统计出中英文大写字符,小写字符,数字,空格,其他字符的个数。
    我们首先想到的的是把这三行文字,先进行合并,然后循环读取每个字符,然后根据字符的类型,进行每种类型字符的统计。根据上述原理,代码编写如下:
 

#include <stdio.h>
#define N 100

int main(int argc, int *argv[])
{
    char str1[N],str2[N],str3[N];
    char str0[300];
    char c;
    int i;
    int j = 0,k = 0,t = 0,m = 0,n = 0;
    printf("please input str1:");
    gets(str1);
    printf("please input str2:");
    gets(str2);
    printf("please input str3:");
    gets(str3);
    
    strcat(str0,str1);
    strcat(str0,str2);
    strcat(str0,str3);
    
    for (i = 0; (c = str0[i]) != '\0'; i++)
    {
        if (c >= 'A' && c <= 'Z')
        {
              j++;
        }
        else if (c >='a' && c <= 'z')
        {
             k++;
        }
        else if (c == ' ')
        {
             t++;
        }
        else if(c >='0' && c <='9')
        {
             m++;
        }
        else
        {
            n++;
        }
    }
    
    printf("upper char : %d ,lower char :%d ,number char :%d ,\nspace char : %d , ohter char : %d\n",
                  j,k,m,t,n);
    system("pause");
    return 0;
    
}


阅读(1446) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~