首页 > 其他 > 详细

PTA 面向对象程序设计6-2 统计数字

时间:2021-04-03 19:50:08      阅读:20      评论:0      收藏:0      [点我收藏+]

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

类和函数接口定义:

设计一个类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 num = 0;
            for(int i=0;i<str.length();i++){
                if(str[i] >= 0 &&str[i] <= 9){
                    num++;
                }
            }
            return num;
        }
};

 

PTA 面向对象程序设计6-2 统计数字

原文:https://www.cnblogs.com/Arisf/p/14613624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!