C++统计字母,空格,字符个数
统计字母,空格,字符个数知识点:while循环和if循环的嵌套使用//要求:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数#include<iostream>using namespace std;int main(){int letter = 0, space = 0, digit = 0, others = 0;char c;while ((c = getchar
·
统计字母,空格,字符个数
知识点: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;
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)