zoukankan      html  css  js  c++  java
  • sicily 6497. 字符统计

    Description
    输入一个字符串,统计此字符串中字母(a-z, A-Z)、数字(0-9)、空格(' '),和其它字符的个数,输出四
    类字符的个数。字符串以回车来结束输入,回车不纳入统计,要求能处理空串。

    Input
     输入包含多个测试用例,第一行为一个正整数t,表示测试用例的个数,以下的t行,每行输入一个字符串(长度不超过500),每行以回车结束。

    Output
     对于每个测试用例,第一行输出Letter及其个数,第二行输出Number及其个数,第三行输出Space及其个数,第四行输出Other及其个数,使用空格分开。注意:两个测试用例之间用一个空行分开。

    用ASCII码的特点做,注意A~Z和a~z之间的ASCII码不是连起来的,还有几个符号

    TA修改过测试用例之后变成用数组长度判断是否统计完毕的方法会RE了,而且输出格式奇葩地变成了最后一行不能换行……

    View Code
     1 #include<stdio.h>
     2 void statistics( const char array[] );
     3 
     4 int main()
     5 {
     6     int t;
     7     int i;
     8     char array[502]= {0};
     9     
    10     scanf ("%d", &t );
    11     getchar();
    12     
    13     for ( i = 1; i <= t; i++ )
    14     { 
    15         gets(array); 
    16         
    17         statistics( array );
    18         
    19         if( i < t ) 
    20         {
    21             printf("\n\n");
    22         }
    23     }
    24     
    25     return 0;
    26 } 
    27 
    28 void statistics( const char array[] )
    29 {
    30     int i;
    31     int letter =0, number = 0, space = 0, other = 0;
    32     
    33     for ( i = 0; array[i] != '\0'; i++ )
    34     {
    35         if ( array[i] >= 'A' && array[i] <= 'Z' )
    36         {
    37             letter++;
    38         }
    39         else if ( array[i] >= 'a' && array[i] <= 'z' ) 
    40         {
    41             letter++;
    42         }
    43         else if ( array[i] >= '0' && array[i] <= '9' )
    44         {
    45             number++;
    46         }
    47         else if ( array[i] == ' ' )
    48         {
    49             space++;
    50         }
    51         else
    52         {
    53             other++;
    54         }
    55     }
    56     
    57     printf( "Letter %d\n", letter );
    58     printf( "Number %d\n", number );
    59     printf( "Space %d\n", space );
    60     printf( "Other %d", other );
    61     
    62     return;
    63 }
  • 相关阅读:
    使用tcmalloc编译启动时宕机
    使用tcmalloc编译出现undefined reference to `sem_init'
    使用AddressSanitizer做内存分析(一)——入门篇
    VIM-美化你的标签栏
    Entity Framework Code First (六)存储过程
    Entity Framework Code First (五)Fluent API
    Entity Framework Code First (四)Fluent API
    Entity Framework Code First (三)Data Annotations
    Entity Framework Code First (二)Custom Conventions
    Entity Framework Code First (一)Conventions
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2797715.html
Copyright © 2011-2022 走看看