A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 悄然林静 中级黑马   /  2016-7-28 23:16  /  988 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

题目:有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。分析:根据不同字符的ASCII码范围设置判定区间,并分别计数。
代码如下:
[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;
int main()
{int i,j,upper,lower,digit,space,other;
 char text[3][80];
 upper=lower=digit=space=other=0;
 for (i=0;i<3;i++)
   {cout<<"please input line "<<i+1<<endl;
    gets(text[i]);
    for (j=0;j<80 && text[i][j]!='\0';j++)
      {if (text[i][j]>='A'&& text[i][j]<='Z')
         upper++;
       else if (text[i][j]>='a' && text[i][j]<='z')
         lower++;
       else if (text[i][j]>='0' && text[i][j]<='9')
         digit++;
       else if (text[i][j]==' ')
         space++;
       else
         other++;
     }
   }
   cout<<"upper case:"<<upper<<endl;
   cout<<"lower case:"<<lower<<endl;
   cout<<"digit     :"<<digit<<endl;
   cout<<"space     :"<<space<<endl;
   cout<<"other     :"<<other<<endl;
   return 0;
} 




2 个回复

倒序浏览
沙发挽尊~~
回复 使用道具 举报
做个板凳来围观
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马