题解 | #使用字符函数统计字符串中各类型字符的个数#
使用字符函数统计字符串中各类型字符的个数
https://www.nowcoder.com/practice/31bdbc70188f48e995fa3cbef36613c8
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int alphabets = 0;
int digits = 0;
int whitespace = 0;
int others = 0;
// panduan zifuchuan
for (int i = 0; i < str.length(); ++i)
{
if (isalpha(str[i]))
{
++alphabets;
}
else if (isdigit(str[i]))
{
++digits;
}
else if (isspace(str[i]))
{
++whitespace;
}
else
{
++others;
}
}
cout << "chars : " << alphabets
<< " whitespace : " << whitespace
<< " digits : " << digits
<< " others : " << others << endl;
return 0;
}
这个就是要知道string类型的一些用法, 并且string 类型可以像一般的字符串一样按下标访问
realme公司福利 338人发布
查看16道真题和解析
