教你夺冠


私信TA

用户名:835685327

访问量:149166

签 名:

相互交流 相互学习

等  级
排  名 13
经  验 21664
参赛次数 0
文章发表 84
年  龄 0
在职情况 学生
学  校 辣鸡施工大学
专  业

  自我简介:

努力刷题 熟能生巧!

有两种判断字符是何种类型的方式。

1) 直接测试字符是何种类型:check1

2) 利用字符分类函数:check2


参考代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void check1(char *str)
{
    int len = strlen(str);
    int abc = 0;
    int num = 0;
    int space = 0;
    int other = 0;

    int i;
    for (i = 0; i < len; i++)
    {
        if (str[i] <= 'z' && str[i] >= 'a')
            abc++;
        else if (str[i] <= 'Z' && str[i] >= 'A')
            abc++;
        else if (str[i] <= '9' && str[i] >= '0')
            num++;
        else if (str[i] == ' ')
            space++;
        else
            other++;
    }

    printf("%d %d %d %d\n", abc, num, space, other);
}

void check2(char *str)
{
    int len = strlen(str);
    int abc = 0;
    int num = 0;
    int space = 0;
    int other = 0;

    int i;
    for (i = 0; i < len; i++)
    {
        if (isalpha(str[i]))
            abc++;
        else if (isdigit(str[i]))
            num++;
        else if (isspace(str[i]))
            space++;
        else
            other++;
    }

    printf("%d %d %d %d\n", abc, num, space, other);
}

int main()
{
    char str[100];
    scanf("%[^\n]", str);

    check1(str);
    check2(str);

    return 0;
}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区