对于给定的一个字符串,统计其中数字字符出现的次数。
2 asdfasdf123123asdfasdf asdf111111111asdfasdfasdfSample Output
6 9
本题的重点是确定字符串的有效位数,而这可以用cstring中strlen关键词来实现
具体的代码如下
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a,num;
cin>>a;
for(int i=0;i<a;i++)
{
char str[800];
cin>>str;
int length=strlen(str);
num=0;
for(int n=0;n<length;n++)
{
if (str[n]>47&&str[n]<58)//use ACSII code to show the amount of the number
num++;
}
cout<<num<<endl;
}
}