【东华大学oj】5 字符串分析(面向对象)

5 字符串分析

作者: 冯向阳时间限制: 1S章节: 其它

问题描述 :

内容:对于任意一个输入的字符串(限定长度在100以内),实现以下功能函数:要求统计其中字符(包括空格)的个数,同时分别指出其中大、小写字母、数字以及其它字符的个数。最后将所有统计结果输出到屏幕上。并编写main函数测试该功能函数。

注意:要求使用指针实现,函数的参数传递采用地址传递、引用传递方式。

函数原型为:

void string_analysis(char *ptr, int &total, int &cap, int &sma, int &num, int &oth);

//total为所有字符的个数,cap为大写字母的个数,sma为小写字母的个数,num为数字的个数,oth为其它字符的个数

main函数可参考以下代码编写:

int main(){ 

   char str[100]; 

    

   int total,cap,sma,num,oth; 

   total = cap = sma = num = oth = 0; 

   cin.get(str,100); 

   string_analysis(str, total, cap, sma, num, oth);

   cout<<cap<<endl; 

   cout<<sma<<endl; 

   cout<<num<<endl; 

   cout<<oth<<endl; 

   cout<<total<<endl; 

   return 0; 

}

输入说明 :

输入的字符串测试数据位于一行。

输出说明 :

对于测试数据,输出对应的分析结果。

第一行:大写字母的个数

第二行:小写字母的个数

第三行:数字的个数

第四行:其它字符的个数

第五行:所有字符的个数

#include <iostream>
using namespace std;

void string_analysis(char *ptr, int& total, int& cap, int& sma, int& num, int& oth) {
    total = cap = sma = num = oth = 0;
    while (*ptr != '\0') { // 遍历字符串,直到遇到字符串尾标志'\0'
        total++;
        if (*ptr >= 'A' && *ptr <= 'Z') { // 大写字母
            cap++;
        } else if (*ptr >= 'a' && *ptr <= 'z') { // 小写字母
            sma++;
        } else if (*ptr >= '0' && *ptr <= '9') { // 数字
            num++;
        } else { // 其它字符
            oth++;
        }
        ptr++;
    }
}

int main() {
    char str[100];
    int total, cap, sma, num, oth;
    total = cap = sma = num = oth = 0;

    cin.get(str, 100);
    string_analysis(str, total, cap, sma, num, oth);

    cout << cap << endl;
    cout << sma << endl;
    cout << num << endl;
    cout << oth << endl;
    cout << total << endl;

    return 0;
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值