肖英豪


私信TA

用户名:dotcpp0602020

访问量:2849

签 名:

睡大觉

等  级
排  名 2151
经  验 2379
参赛次数 7
文章发表 61
年  龄 0
在职情况 学生
学  校 内蒙古工业大学
专  业

  自我简介:

TA的其他文章

解题思路:

淦,还是面向结果编程简单,我这是自讨苦吃
主要还是先函数多传几个数
其实除了结构体,还可以用指针,数组。。。
不过,直接面向结果编,这道题就没必要写了

注意事项:

自定义函数
在主函数中输入字符串以及输出上述结果
自定义函数负责处理数据

参考代码:

#include <iostream>
#include <string>
using namespace std;
struct arr
{
    int letter = 0;
    int num = 0;
    int space = 0;
    int ch = 0;
};

arr  func( string s )
{
    struct arr ans;
    for ( int i = 0; i < s.size(); i++ )
    {
        if ( s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z' ) 
            ans.letter++;
        else if ( s[i] >= '0' && s[i] <= '9' )
            ans.num++;
        else if ( s[i] == ' ' )
            ans.space++;
        else
            ans.ch++;      
    }

    return ans; 
}
int main()
{
    string 
    getline( cin, s );
   
    arr ans = func( s );

    cout << ans.letter << ' ' << ans.num << ' ' << ans.space << ' ' << ans.ch; 
    return 0;
}
 

0.0分

1 人评分

  评论区