牵风 发表于 2021-10-21 19:49:42

统计英文字母空格或回车数字字符和其它字符个数

#include<stdio.h>
int main()
{
        int n;
        char ch;
        int num,c,symbol,other;
       
    scanf("%d",&n);
    scanf("%c",&ch);
    num=0;
    c=0;
    symbol=0;
    other=0;
    while(((ch=getchar())!='\n'))
    {
    if(n>=0&&n<=9)
    {
            num++;
        }
        else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
        {
                c++;
        }
        else if(ch=='\n'||ch=='\t')
        {
                symbol++;
        }
        else
        {
                other++;
        }
    }
    printf("%d %d %d %d",num,c,symbol,other);
   
    return 0;
       
}

jackz007 发表于 2021-10-21 20:15:26

#include <stdio.h>

int main(void)
{
      inta , b , d , o , s                                                   ;
      char c                                                                     ;
      for(a = b = d = o = s = 0 ; (c = getchar())!= '\n' ;) {
                if(c >= ' ' && c <= '~') {
                        if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) a ++;
                        else if(c == ' ') b ++                                     ;
                        else if(c >= '0' && c <= '9') d ++                         ;
                        else s ++                                                ;
                } else {
                        o ++                                                       ;
                }
      }
      printf("alphabet : %d\n" , a)                                              ;
      printf("   blank : %d\n" , b)                                              ;
      printf("   digit : %d\n" , d)                                              ;
      printf("symbol : %d\n" , s)                                              ;
      printf("   other : %d\n" , o)                                              ;
}
      编译、运行实况:
D:\00.Excise\C>cl x.c
用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
版权所有(C) Microsoft Corporation。保留所有权利。

x.c
Microsoft (R) Incremental Linker Version 14.28.29334.0
Copyright (C) Microsoft Corporation.All rights reserved.

/out:x.exe
x.obj

D:\00.Excise\C>x
SFEWW@#^*)ytjykil{};".<Mn~!@#RT&*&9054
alphabet : 16
   blank : 0
   digit : 4
symbol : 18
   other : 0

D:\00.Excise\C>

傻眼貓咪 发表于 2021-10-21 20:20:47

#include <stdio.h>
#include <ctype.h>

int main()
{
    int n;
    char c;
    int letters = 0;
    int space = 0;
    int newline = 0;
    int digits = 0;
    int symbol = 0;
   
    scanf("%d", &n);
    getchar();
   
    for(int i = 0; i < n; i++){
      scanf("%c", &c);
      if(isalpha(c)) letters++;
      else if(c == ' ') space++;
      else if(c == '\n') newline++;
      else if(isdigit(c)) digits++;
      else symbol++;
    }
   
    printf("英文字母:%d\n空格:%d\n回车:%d\n数字:%d\n其它字符:%d", letters, space, newline, digits, symbol);
    return 0;
}banana
43 H$#
英文字母:7
空格:1
回车:1
数字:2
其它字符:2

傻眼貓咪 发表于 2021-10-21 20:24:42

jackz007 发表于 2021-10-21 20:15
编译、运行实况:

请问如果是回车呢?你的代码回车就结束了,无法算出回车次数{:5_94:}

傻眼貓咪 发表于 2021-10-21 20:26:11

jackz007 发表于 2021-10-21 20:15
编译、运行实况:

请问如果是回车呢?你的代码回车就结束了,无法算出回车次数{:5_94:}{:7_146:}{:7_146:}{:7_146:}{:7_146:}{:7_146:}

jackz007 发表于 2021-10-21 20:29:27

傻眼貓咪 发表于 2021-10-21 20:24
请问如果是回车呢?你的代码回车就结束了,无法算出回车次数

       自然是不能,因为需要设定键盘输入的终点,本代码设定以回车符作为键盘输入的终结,所以,回车符自然就无法被纳入统计。

傻眼貓咪 发表于 2021-10-21 20:55:04

jackz007 发表于 2021-10-21 20:29
自然是不能,因为需要设定键盘输入的终点,本代码设定以回车符作为键盘输入的终结,所以,回车符 ...

其实楼主题目有个 n 整数,就是输入字符长度
页: [1]
查看完整版本: 统计英文字母空格或回车数字字符和其它字符个数