请编写程序,实现以下功能: 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。(c语言的!)

请编写程序,实现以下功能:

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

有没有人会,帮忙编写一个c语言程序,语言写一个后缀为.c的c语言程序

供参考:

#include <stdio.h>
#include <ctype.h>
int main()
{
    char str[128];
    int  i = 0, letter = 0, space = 0, num = 0, other = 0;
    gets(str);
    while (str[i]) {
             if (isalpha(str[i])) letter++;
        else if (isspace(str[i])) space++;
        else if (isdigit(str[i])) num++;
        else other++;
        i++;
    }
    printf("Letter:%d ,Space:%d ,Num:%d ,Other:%d", letter, space, num, other);
    return 0;
}

#include<stdio.h>
int main()
{
    char c;
    int letter = 0, space = 0, digit = 0, other = 0;
    printf("请输入字符串:\n");
    while ((c = getchar()) != '\n')
    {
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
            letter++;
        else if (c == ' ')
            space++;
        else if (c >= '0' && c <= '9')
            digit++;
        else
            other++;
    }
    printf("letter=%d\nspace=%d\ndigit=%d\nother=%d", letter, space, digit, other);
    return 0;
}
 

下面是一个实现,供参考:

#include<stdio.h>

int main(void){
    
    char str[100]; //存放一行字符串输入 
    gets(str) ;
    
    int i=0; //访问字符数组的下标 
    char ch;  //存放访问到的字符数组每个位置的字符 
    int alphabet=0,space=0,number=0,other=0;  //存放字母、空格、数字和其他字符数量的变量 
    
    while(str[i]!='\0'){  //如果字符串没有结束 
        ch=str[i];  //获取字符数组当前位置的字符 
        if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){  //如果是大写字母或小写字母 
            alphabet++;
        }else if(ch>='0'&&ch<='9'){  //如果是数字 
            number++;
        }else if(ch==' '){  //如果是空格 
            space++;
        }else{  //如果是其他字符 
            other++;
        }
        i++;  //指向字符数字下一个位置 
    }
    
    //打印统计结果 
    printf("字符串统计情况:英文字母%d个,空格%d个,数字%d个,其他字符%d个。\n",
        alphabet,space,number,other);
        
    return 0;
    
} 

img

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

//汉字是双字节的编码格式!
//汉字的第一字节:是从0xB0   开始编码
//汉字的第二字节:是从0xA1   开始编码
//有了以上的了解,就不难识别出是中文,还是其他的编码!
//程序中只需要判断(第一字节 >= 0xB0 && 第二字节 >= 0xA1)

int is_zh_ch(char* p)//判断是否是汉字
{
    char c = *p++;

    if (c & 0x80)
    {  //如果字符高位为1且下一字符高位也是1则有中文字符
        if (*p & 0x80) {
            return 1;
        }
    }

    return 0;
}


int main()
{
    char arr[1000];
    gets(arr);
    int sz = strlen(arr);
    int countEN = 0, countNUM = 0, countOTHER = 0, countEMPTY = 0,countCN=0;

    for (int i = 0; i < sz; i++)
    {
        if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z'))
        {
            countEN++;
        }
        else if (arr[i] >= '0' && arr[i] <= '9')
        {
            countNUM++;
        }
        else if (arr[i] == ' ')
        {
            countEMPTY++;
        }
        else if (is_zh_ch(&arr[i]))
        {
            countCN++;
            i++;
        }
        else
        {
            countOTHER++;
        }
    }

    printf("%d\n", countEN);//英文
    printf("%d\n", countNUM);//数字
    printf("%d\n", countCN);//中文
    printf("%d\n", countEMPTY);//空格
    printf("%d\n", countOTHER);//其他
    return 0;
}

img

不显示的问题,如果是输入之后程序框直接关闭了,你在其他人的main函数的末尾,printf之后return之前加一条语句:

    exit(0);

如果是其他原因请描述一下现象

麻烦各位直接把数字字母空格字符加进去 随便加多少都行 直接让程序显示有多少个