返回一个字符串中数字的个数
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
int T;
cin>>T;
string s;
getline(cin,s); //读掉回车符号
while(T--)
{
getline(cin,s);
int cnt=0;
for(int i=0;i<s.size();i++)
{
if(isdigit(s[i])) cnt++;//在头文件 cctype 中
}
cout<<cnt<<endl;
}
}
测试结果:
输入
5
hg2323
2g3332332
jhj222222kjkjkj22222
dyuysddysdu1234567890
hsjdu12345678901234567890jdshjgshgds
输出
4
8
11
10
20
运行时间:1ms