统计字母,空格,字符个数
知识点:while循环和if循环的嵌套使用
//要求:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
#include<iostream>
using namespace std;
int main()
{
int letter = 0, space = 0, digit = 0, others = 0;
char c;
while ((c = getchar()) != '\n')
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letter++;
else if (c == ' ')
space++;
else if (c >= '0' && c <= '9')
digit++;
else others++;
}
cout << "字母总数为:" << letter << endl;
cout << "空格总数为:" << space << endl;
cout << "数字总数为:" << digit << endl;
cout << "其他字符总数为:" << others << endl;
return 0;
}