包括统计字符,空格,制表符,换行符:

#include <stdio.h>
#include <Conio.h>
main(){
      /* count characters, spaces & lines in input */
      double lines,chars,spaces,tabs;
      int input;
      lines = 0;
      while((input=getchar())!=EOF){
         if(input == '\n'){++lines;}
         if(input == ' '){++spaces;}
         if(input == '\t'){++tabs;}
         ++chars;
      }
      printf("lines is %.0f, spaces is %.0f, tabs is %.0f, characters is %.0f\n",
                    lines,spaces,tabs, chars);
      getch();
}

输入:制表符hello空格world换行

        空格空格123制表符!换行

结果:

字符统计

tips:

单引号中的字符表示一个整型值,该值等于此字符在机器字符集中对应的数值,我们称之为字符常量。但是,它只不过是小的整型数的另一种写法而已。例如,'A'是一个字符常量;

在ASCII字符集中其值为65(即字符A的内部表示值为65)。当然,用'A'要比用65 好,因为。'A'的意义更清楚,且与特定的字符集无关。

字符串常量中使用的转义字符序列也是合法的字符常量,比如,'\n'代表换行符的值,在ASCII字符集中其值为10。我们应当注意到,'\n'是单个字符,在表达式中它不过是一个整型

数而已;而"\n"是一个仅包含一个字符的字符串常量。

 

相关文章:

  • 2021-09-16
  • 2021-06-12
  • 2021-09-10
  • 2022-01-06
  • 2021-07-16
  • 2021-08-19
  • 2021-11-25
  • 2021-05-24
猜你喜欢
  • 2022-12-23
  • 2021-12-16
  • 2021-04-16
  • 2021-04-09
  • 2021-08-02
相关资源
相似解决方案