首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:155032 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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

数据范围:输入的字符串长度满足


输入描述:

输入一行字符串,可以有空格



输出描述:

统计其中英文字符,空格字符,数字字符,其他字符的个数

示例1

输入

1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][

输出

26
3
10
12
#include <ctype.h>
#include <stdio.h>

int main() {
    char c;
    int cnt_alpha = 0;
    int cnt_num = 0;
    int cnt_blank = 0;
    int cnt_oth = 0;

    while ((c = getchar()) != EOF) {
        if(isalpha(c)){
            cnt_alpha++;
        }else if(isdigit(c)){
            cnt_num++;
        }else if(isblank(c)){
            cnt_blank++;
        }else if('\n' == c){
            continue;
        }else{
            //printf("[%c]\n",c);
            cnt_oth++;
        }
    }

    printf("%d\n", cnt_alpha);
    printf("%d\n", cnt_blank);
    printf("%d\n", cnt_num);
    printf("%d\n", cnt_oth);

    return 0;
}

编辑于 2024-02-21 09:15:30 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char s[1002];
    int gn=0;
    int nn=0;
    int an=0;
    int kn=0;
    fgets(s, 1002, stdin);
    for(int i=0;i<strlen(s)-1;i++){
        switch (s[i]) {
            case 'A' ... 'Z':
            case 'a'...'z':
            an+=1;
            break;
            case ' ':
            nn++;
            break;
            case '0' ... '9':
            kn++;
            break;
            default: gn++;
        }
    }
    printf("%d\n%d\n%d\n%d\n",an,nn,kn,gn);
    return 0;
}
编辑于 2023-12-14 17:24:27 回复(0)
#include <stdio.h>
# include<string.h>
int main() {
    int a[4] = {0};char s[1001] = {'\0'};
    gets(s);
    for(int i = 0;s[i] != '\0'; i++){
        if(islower(s[i]) || isupper(s[i])) a[0]++;
        else if(s[i] == 32) a[1]++;
        else if(47<s[i] && s[i]<58) a[2]++;
        else a[3]++;
    }
    for(int i = 0; i<4; i++) printf("%d\n",a[i]);
    return 0;
}

发表于 2023-12-02 22:36:20 回复(0)
//太简单了,遍历一遍就可以了
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char str[1001] = {'\0'};
    gets(str);
    int len = strlen(str);
    int alpha = 0;
    int space = 0;
    int digit = 0;
    int others = 0;
    for(int i = 0;i<len;i++)
    {
        if(isdigit(str[i]))
        {
            digit++;
        }
        else if(isalpha(str[i]))
        {
            alpha++;
        }
        else if(str[i] - ' ' == 0)
        {
            space++;
        }
        else {
        others++;
        }
    }
    printf("%d\n%d\n%d\n%d\n",alpha,space,digit,others);
    return 0;
}

发表于 2023-10-10 21:23:58 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char str[1001]={0};
    gets(str);
    int len=strlen(str);
    int english,blank,number,other;
    english=blank=number=other=0;
    for(int i=0;i<len;i++)
    {
        if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')//英文字母
            english++;
        else if (str[i]==' ')//空格
            blank++;
        else if (str[i]>='0'&&str[i]<='9')//数字
            number++;
        else//其他
            other++;
    }
    printf("%d\n%d\n%d\n%d\n",english,blank,number,other);
    return 0;
}
发表于 2023-09-03 22:00:08 回复(0)
#include <stdio.h>

int main(void)
{
  char str[1000], *p, word[128] = {0};
  int e = 0, d = 0, s = 0, o = 0, i;
  
  while(scanf("%s,", str) != EOF)
  {
    p = str;
    do
    {
      word[*p]++;
      o++;
    }while(*(++p));
    word[0]++;
  }
  
  for(i = 'A'; i <= 'Z'; i++)  e += word[i] + word[i + 32];
  for(i = '0'; i <= '9'; i++)  d += word[i];
  
  printf("%d\r\n%d\r\n%d\r\n%d", e, word[0] - 1, d, o - e - d);
}

发表于 2022-08-16 17:31:14 回复(0)
#include <stdio.h>
#define        N        1000
int main()
{
    char str[N];
    int letter=0,space=0,number=0,other=0,i=0;
    gets(str);
    while(str[i]!='\0')
    {
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
        {
            letter++;
        }
        else if(str[i]==' ')
        {
            space++;
        }
        else if(str[i]>='0'&&str[i]<='9')
        {
            number++;
        }
        else
        {
            other++;
        }
        i++;
    }
    printf("%d\n%d\n%d\n%d\n",letter,space,number,other);
    return 0;
}

发表于 2022-04-21 12:06:31 回复(0)
#include<stdio.h>
#include<string.h>
int main() {
    char str[1001] = {'\0'};
    while (gets(str)) {
        int len = strlen(str);
        int charac = 0, blank = 0, num = 0, other = 0;
        for (int i = len - 1; i >= 0; i--) {
            //字母计数(区分大小写)
            if ((('a' <= str[i])&&(str[i] <= 'z')) || (('A' <= str[i])&&(str[i] <= 'Z'))) charac++;
            else if (str[i] == ' ') blank++;  //空格计数
            else if (('0' <= str[i]) && (str[i] <= '9')) num++;  //数字计数
            else other++;  //其他字符计数
        }
        printf("%d\n%d\n%d\n%d\n", charac, blank, num, other);
    }
}

发表于 2022-03-31 05:16:47 回复(0)
#include<stdio.h>
#include <string.h>
#define MAX=10000
int main(){
    char a[1000];
    gets(a);
     int len=strlen(a),b=0,c=0,d=0,e=0;
    for(int i=0;i<len;i++){
       
        if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')){
            b++;
        }
        else if(a[i]==32){
            d++;
        }
        else if(a[i]>='0'&&a[i]<='9'){
            c++;
        }
       else e++;
       
    }  
 
    printf("%d\n%d\n%d\n%d\n",b,d,c,e);
    }

发表于 2022-03-19 10:00:22 回复(0)
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000

int main()
{
    char ch[MAX_SIZE];
    while(gets(ch)){
        int alphabet = 0;
        int space = 0;
        int number = 0;
        int other = 0;
        for (int i = 0; ch[i] != '\0'; i++)
        {
            char c = ch[i];
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            {
                alphabet++;
            }
            else if (c >= '0' && c <= '9')
            {
                number++;
            }
            else if (c == ' ')
            {
                space++;
            }
            else
            {
                other++;
            }
        }
        printf("%d\n%d\n%d\n%d\n", alphabet, space, number, other);
    }
    return 0;
}

发表于 2021-11-15 05:30:09 回复(0)
#include<stdio.h>

int main(void)
{
    char str[500]={0};
    int len = 0;

    while(gets(str)){
        int i =0,a=0,b=0,c=0,d=0;
        len = strlen(str);
        for(i=0;i<len;i++){
            if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) {
                a++;
            } else if(str[i] == ' ') {
                b++;
            } else if(str[i]>='0'&&str[i]<='9') {
                c++;
            } else
                d++;
        }
        printf("%d\n%d\n%d\n%d\n",a,b,c,d);
    }
    return 0;
}
发表于 2021-09-24 22:46:23 回复(0)
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char str[256] = {0};
    char *p = NULL;
    int space = 0;
    int alpha = 0;
    int num = 0;
    int eth = 0;
    
    gets(str);
    p = str;
    while (*p != '\0')
    {
        if(*p == 32)
        {
            space++;
        }
        else if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
        {
            alpha++;
        }
        else if (*p >= 48 && *p <= 57)
        {
            num++;
        }
        else
        {
            eth++;
        }
        p++;
    }
    printf("%d\n%d\n%d\n%d\n", alpha, space, num, eth);
    
    return 0;
}

发表于 2021-09-17 20:01:22 回复(0)
无难度的题
#include<stdio.h>
int main(){
    char in[500];
    while (gets(in)) {
        int yw=0, kg=0, sz=0, qt=0;
        for (int i = 0; i < strlen(in); i++) {
            if (in[i] == ' ') {
                kg++;
            } else if ((in[i] >= 'A' && in[i] <= 'Z') || in[i] >= 'a' && in[i] <= 'z') {
                yw++;
            } else if (in[i] >= '0' && in[i] <= '9') {
                sz++;
            } else {
                qt++;
            }
        }
        printf("%d\n%d\n%d\n%d\n", yw, kg, sz, qt);
    }
}
发表于 2021-08-14 23:19:20 回复(0)

问题信息

难度:
13条回答 45325浏览

热门推荐

通过挑战的用户

查看代码