C语言C++数组字符串

相关知识
数组的元素是字符
编程要求
本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下:编程实现:输入一个字符串(长度不超过90),分别统计其中数字、空格、字母及其他字符(除数字、字母、空格之外的)出现的次数。
测试说明
以下是测试样例:输入: China 1949.10.1~2010.10.1 输出:数字:14 空格:1 字母:5 其他字符:5
#include <stdio.h>
int main()
{
    // 请在此添加代码
    /********** Begin *********/

    
    /********** End **********/

    return 0;
}

    char str[90];
    int num = 0, ch = 0, space = 0, other = 0,i=0;

    printf("please enter the str: ");
    scanf("%[^\n]",str);//带空格输入

    while(str[i] != '\0')
    {
        if(str[i] >= '0' && str[i] <= '9')
            num++;
        else if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )
            ch ++;
        else if(str[i] == ' ')
            space ++;
        else 
            other ++;

        i++;
    
    }
    
    printf("数字: %d 空格: %d 字母: %d 其他字符: %d\n",num,space,ch,other);

测试有效,希望采纳


char s[100];
    fgets(s,100,stdin);
    int a=0,b=0,c=0,d=0;
    
    for(int i=0;s[i] != '\0';i++){
        if((s[i]>='A' && s[i]<='Z')||(s[i]>='a' && s[i]<='z')){
            a++;
        }
        else if(s[i]==' '){
            b++;
        }
        else if(s[i]>='0' && s[i]<='9'){
            c++;
        }
        else{
            d++;
        }
    }
    printf("数字:%d 空格:%d 字母:%d 其他字符:%d",c,b,a,d);