字符类型统计器
请编写一个C程序,在终端用键盘输入字符串,以Ctrl+Z组合键表示输入完毕,统计输入的字符串中空格符,制表符,换行符的个数,并显示统计结果。
题目分析:
解决字符的分类问题可以通过字符的ASCII码进行判断。
Ctrl+Z的组合键输入的字符在计算机中对应的是EOF结束标志。
#include<string.h>
#include<stdio.h>
main()
{
char c;
int space=0,table=0;enter=0;
printf("Please input a string:\n");
scanf("%c",&c);
while(c!=EOF)
{
switch(c)
{
case 32:space++;break;
case 9:table++;break;
case 10:enter++;break;
default:break;
}
scanf("%c",&c)
}
printf("The number of space is %d\n",space);
printf("The number of table is %d\n",table);
printf("The number of enter is %d\n",enter);
getchar();
teturn 0;
}
getchar();是用来接收回车的