云计算百科
云计算领域专业知识百科平台

面向对象程序设计-模板- 统计数字

作者 李廷元

单位 中国民用航空飞行学院

对于给定的一个字符串,统计其中数字字符出现的次数。

类和函数接口定义:

设计一个类Solution,其中包含一个成员函数count_digits,其功能是统计传入的string类型参数中数字字符的个数并返回。

裁判测试程序样例:

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <iostream>
using namespace std;

/* 请在这里填写答案 */

int main()
{
int t;

cin >> t;
getchar();
while (t–)
{
string str;
Solution obj;

getline(cin,str);
int digits = obj.count_digits(str);

cout << digits << endl;
}

return 0;
}

输入样例:

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

输出样例:

6
9

参考示例:

class Solution {
public:
int count_digits(string str) {
int count=0;
for (int i=0;i<str.length();i++) {
if (str[i]>='0'&&str[i]<='9') {
count++;
}
}
return count;
}
};

赞(0)
未经允许不得转载:网硕互联帮助中心 » 面向对象程序设计-模板- 统计数字
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!