我想用 C++ 编写一个程序,它从用户那里获取一个字符串,并输出大写字母、小写字母和数字的数量。
例如对于输入“aKb12CD34”,结果应为 2 个小写字母、3 个大写字母和 4 个数字。
Try:
#include <algorithm>
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main()
{
cout << " Enter text: ";
string s;
if(getline(cin, s))
{
size_t count_lower = count_if(s.begin(), s.end(),
[](unsigned char ch) { return islower(ch); });
cout << "lowers: " << count_lower ;
size_t count_upper = count_if(s.begin(), s.end(),
[](unsigned char ch) { return isupper(ch); });
cout << "uppers: " << count_upper ;
size_t count_digit = count_if(s.begin(), s.end(),
[](unsigned char ch) { return isdigit(ch); });
cout << "digits: " << count_digit ;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)