#include <iostream>
using namespace std;
int main()
{
char c;
int letter=0,space=0,digh=0,other=0;
cout<<"请输入一行字符:";
while ((c=getchar())!='\n')//一开始是while (c=getchar()!='\n')非!=的运算优先级高于=,所以输出地c非0即1
{
if (c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digh++;
else
other++;
}
cout<<"letter:"<<letter<<"space:"<<space<<"digh:"<<digh<<"other:"<<other<<endl;
return 0;
}

本文介绍了一个简单的C++程序,用于统计用户输入的一行字符中字母、空格、数字和其他字符的数量。通过使用基本的循环和条件判断,该程序能够有效地进行字符分类并计数。
9763

被折叠的 条评论
为什么被折叠?



