题目描述
从键盘中任意输入一串字符,直至输入"#"字符代表结束.请编程统计输入的字符中的大写字母,小写字母和数字字符的个数分别是多少?
输入
输入只有一行,包括一串字符.(长度小于20)
输出
输出只有一行(这意味着末尾有一个回车符号),包括3个整数。分别代表大写字符,小写字符和数字字符的个数。
样例输入 Copy
daDSALDdcada3240#
样例输出 Copy
5 7 4
参考:
#include <iostream>
using namespace std;
int main()
{
char ch;
int A=0,a=0,digit=0;
cin>>ch;
while(ch!='#')
{
if(ch>='0'&&ch<='9')digit++;
if(ch>='a'&&ch<='z')a++;
if(ch>='A'&&ch<='Z')A++;
cin>>ch;
}
cout <<digit << ' ' << a << ' ' << A << endl;
return 0;
}
代码如下,题主可作参考:
#include<stdio.h>
int main()
{
char ch;
int A=0,a=0,digit=0;
while((ch=getchar())!='#')
{
if(ch>='0'&&ch<='9')digit++;
if(ch>='a'&&ch<='z')a++;
if(ch>='A'&&ch<='Z')A++;
}
printf("%d %d %d\n",A,a,digit);
return 0;
}
#include <iostream>
#include <iterator>
#include <ranges>
using namespace std;
int main()
{
int numUpperLetters = 0, numLowerLetters = 0, numDigits = 0;
for (auto c : ranges::subrange(istream_iterator<char>(cin), istream_iterator<char>()))
{
if (c == '#')
break;
if (isupper(c))
numUpperLetters++;
else if (islower(c))
numLowerLetters++;
else if (isdigit(c))
numDigits++;
}
cout << numUpperLetters << ' ' << numLowerLetters << ' ' << numDigits << endl;
return 0;
}
$ g++ -Wall -std=c++20 main.cpp
$ ./a.out
daDASLDdcada3240#
5 7 4