问题及代码
/*
烟台大学计算机与控制工程学院
文件名称: 统计出其中英文字母、数字、空格和其他字符的个数
作 者:展一
完成时间:2016年10月27日
题目描述
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
输入
一行字符
输出
统计值
样例输入
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
样例输出
23 16 2 4
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int letter=0,number=0,space=0,other=0;
char x;
while((x=getchar())!='\n')
{
if((x>='a'&&x<='z')||(x>='A'&&x<='Z'))
letter++;
else
if(x>='0'&&x<='9')
number++;
else
if(x==' ')
space++;
else
other++;
}
printf("%d %d %d %d\n",letter,number,space,other);
return 0;
}
运行结果
知识点总结
字符型数据使用
利用循环结构进行统计
学习心得
一开始使用的scanf()!=EOF并没有输出结果,之后改成getchar()!=‘\n’程序才可以输出结果。