题解 | #使用字符函数统计字符串中各类型字符的个数#
使用字符函数统计字符串中各类型字符的个数
https://www.nowcoder.com/practice/31bdbc70188f48e995fa3cbef36613c8
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
getline(cin, str);
int whitespace = 0;
int digits = 0;
int chars = 0;
int others = 0;
// write your code here......
for(int i = 0;i<str.size();i++)
{
if(isalpha(str[i]))
{
chars++;
}
else if(isdigit(str[i]))
{
digits++;
}
else if(ispunct(str[i]))
{
others++;
}
else if(isspace(str[i]))
{
whitespace++;
}
}
cout << "chars : " << chars
<< " whitespace : " << whitespace
<< " digits : " << digits
<< " others : " << others << endl;
return 0;
}
查看16道真题和解析