//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
int engNum=0,spacNum=0,digitNum=0,others=0;
char c;
cout<<"Please input some characters : ";
while((c=
getchar())!='\n'){
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
engNum++;
}else if(c>='0'&&c<='9'){
digitNum++;
}else if(c==' '){
spacNum++;
}else{
others++;
}
}
cout<<"The number of elphabet is "<<engNum<<endl;
cout<<"The number of digit is "<<digitNum<<endl;
cout<<"The number of space is "<<spacNum<<endl;
cout<<"The number of others figure is "<<others<<endl;
return 0;
}
//主要学习的是getchar()使用,
一定要有#include "stdio.h"
运行结果为:
- class='magplus' title='点击查看原始大小图片' />
- 大小: 31.1 KB