PATST


私信TA

用户名:PSTST

访问量:2964

签 名:

等  级
排  名 6868
经  验 1313
参赛次数 0
文章发表 6
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:在自定义函数中用到if语句的多分支嵌套结构,分四种情况统计字符串中字母、数字、空格和其他字符的个数

注意事项:自定义函数sort()中,形参_a,b,c,d应按地址传递,即应加上‘&’
参考代码:

#include<iostream>

using namespace std;

int sort(char a[],int &_a,int &b,int &c,int &d);   //自定义函数sort()的声明

int main()

{

   char str[100];

   int a=0,b=0,c=0,d=0;

   gets(str);

   sort(str,a,b,c,d);                               //调用sort()函数

   cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;

   return 0;

}

int sort(char a[],int &_a,int &b,int &c,int &d)     //定义函数sort()

{

   int i;

   for(i=0;a[i]!='\0';i++)

  {

      if(a[i]>='A'&&a[i]<='Z'||a[i]>='a'&&a[i]<='z')

                _a++;

      else if(a[i]>='0'&&a[i]<='9')

                b++;

      else if(a[i]==' ')

                c++;

      else

                d++;

  }

  return _a,b,c,d;                                            //返回值到主函数

}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区