C语言:编写一个程序统计输入字符串中,各个数字空白字符以及其他所有字符出现的次数。

Posted hanjing_1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言:编写一个程序统计输入字符串中,各个数字空白字符以及其他所有字符出现的次数。相关的知识,希望对你有一定的参考价值。

#include<stdio.h>

int main()

     int c = 0;
     int num_count = 0;
     int emp_count = 0;
     int els_count = 0;
     
     while((c = getchar()) != EOF)
     
          if((c >= '0')&&(c <= '9'))
          
               num_count ++ ;
           
          else if(c == ' ')
          
               emp_count ++; 
          
          else
          
               els_count ++; 
          
     
     
     printf("%d %d %d",num_count,emp_count,els_count);
     return 0; 

 

 

另外,方法2相比优与方法1

(1)可计算出每个数字具体有几次。

(2)对于空格使用了函数isspace()。


#include<stdio.h>


int main()

     int c = 0;
     int num_count = 0;
     int emp_count = 0;
     int els_count = 0;
     int arr[10] =0;
     int i = 0;
     
     while((c = getchar()) != EOF)
     
          if(c >= '0'&&c <= '9')
          
               arr[c-'0'] ++;    
           
          else if(isspace(c))
          
               emp_count ++;
          
          else
          
               els_count ++; 
          
     
     
     printf("emp_count: %d\\n",emp_count);
     printf("els_count: %d\\n",els_count);
     
     for( ; i<10;i++)
     
          printf("%d:%d\\n", i, arr[i]); 
     
     return 0; 

 

以上是关于C语言:编写一个程序统计输入字符串中,各个数字空白字符以及其他所有字符出现的次数。的主要内容,如果未能解决你的问题,请参考以下文章