鱼C论坛

 找回密码
 立即注册
查看: 2275|回复: 6

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

[复制链接]
发表于 2021-10-21 19:49:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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;
       
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-21 20:15:26 | 显示全部楼层
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int  a , b , d , o , s                                                     ;
  5.         char c                                                                     ;
  6.         for(a = b = d = o = s = 0 ; (c = getchar())!= '\n' ;) {
  7.                 if(c >= ' ' && c <= '~') {
  8.                         if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) a ++  ;
  9.                         else if(c == ' ') b ++                                     ;
  10.                         else if(c >= '0' && c <= '9') d ++                         ;
  11.                         else s ++                                                  ;
  12.                 } else {
  13.                         o ++                                                       ;
  14.                 }
  15.         }
  16.         printf("alphabet : %d\n" , a)                                              ;
  17.         printf("   blank : %d\n" , b)                                              ;
  18.         printf("   digit : %d\n" , d)                                              ;
  19.         printf("  symbol : %d\n" , s)                                              ;
  20.         printf("   other : %d\n" , o)                                              ;
  21. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>cl x.c
  2. 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
  3. 版权所有(C) Microsoft Corporation。保留所有权利。

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

  7. /out:x.exe
  8. x.obj

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

  16. D:\00.Excise\C>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-21 20:20:47 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <ctype.h>

  3. int main()
  4. {
  5.     int n;
  6.     char c;
  7.     int letters = 0;
  8.     int space = 0;
  9.     int newline = 0;
  10.     int digits = 0;
  11.     int symbol = 0;
  12.    
  13.     scanf("%d", &n);
  14.     getchar();
  15.    
  16.     for(int i = 0; i < n; i++){
  17.         scanf("%c", &c);
  18.         if(isalpha(c)) letters++;
  19.         else if(c == ' ') space++;
  20.         else if(c == '\n') newline++;
  21.         else if(isdigit(c)) digits++;
  22.         else symbol++;
  23.     }
  24.    
  25.     printf("英文字母:%d\n空格:%d\n回车:%d\n数字:%d\n其它字符:%d", letters, space, newline, digits, symbol);
  26.     return 0;
  27. }
复制代码
  1. banana
  2. 43 H$#
  3. 英文字母:7
  4. 空格:1
  5. 回车:1
  6. 数字:2
  7. 其它字符:2
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-21 20:24:42 | 显示全部楼层
jackz007 发表于 2021-10-21 20:15
编译、运行实况:

请问如果是回车呢?你的代码回车就结束了,无法算出回车次数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-21 20:26:11 | 显示全部楼层
jackz007 发表于 2021-10-21 20:15
编译、运行实况:

请问如果是回车呢?你的代码回车就结束了,无法算出回车次数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

       自然是不能,因为需要设定键盘输入的终点,本代码设定以回车符作为键盘输入的终结,所以,回车符自然就无法被纳入统计。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

其实楼主题目有个 n 整数,就是输入字符长度
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-17 13:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表