TeganZayne


私信TA

用户名:Tegan

访问量:6142

签 名:

努力一定会成功

等  级
排  名 3471
经  验 1851
参赛次数 0
文章发表 9
年  龄 20
在职情况 学生
学  校 东大国际
专  业

  自我简介:

解题思路:
编写函数,给定字符上下限,统计字符串中属于该范围字符的个数




注意事项:
字符数组元素个数分配少了可能运行出错




参考代码:

#include <stdio.h>

#include <string.h>

int cnt(char low,char high,char *a);

int main()

{

    char a[128];

    int alp=0,num=0,spc=0,oth=0;

    gets(a);


    alp=cnt('a','z',a)+cnt('A','Z',a);

    num=cnt('0','9',a);

    spc=cnt(' ',' ',a);

    oth=strlen(a)-alp-num-spc;//字符串长度减去字母、数字、空格即为其他字符的总数


    printf("%d %d %d %d\n",alp,num,spc,oth);

    return 0;

}

int cnt(char low,char high,char *a)//比对字符串每个字符是否在low~high范围内

{

    int i,n,sum=0;

    n=strlen(a);

    for(i=0;i<n;i++)

    {

        if(a[i]>=low&&a[i]<=high)

        {

            sum++;

        }

    }

    return sum;

}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区