题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int size = 100;
char ch[size];
int CharNum = 0, DigNum = 0, NullNum = 0, OtherNum = 0;
for(int i = 0; i < size; i++)
{
ch[i] = getchar();
if(ch[i] == '\n') //终止输入
{
break;
}
}
for(int i = 0; i < size && ch[i] != '\n'; i++)
{
if( ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z')
{
CharNum++;
}
else if(ch[i] >= '0' && ch[i] <= '9')
{
DigNum++;
}
else if(ch[i] == ' ')
{
NullNum++;
}
else
{
OtherNum++;
}
}
cout << "字母个数: " << CharNum << endl << "数字个数:" << DigNum << endl
<< "空格个数: " << NullNum << endl << "其他字符: " << OtherNum << endl;
return 0;
}