首页 > 其他 > 详细

getchar()解决“输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数”

时间:2020-01-23 14:10:24      阅读:196      评论:0      收藏:0      [点我收藏+]

  (1)getchar()函数的作用是从计算机终端(一般为键盘)获取一个无符号字符。getchar()函数只能接收一个字符,其函数值就是从输入设备获取到的字符。

该函数声明在stdio.h头文件中,使用的时候要包含stdio.h头文件。如:

  #include<stdio.h>

  int getchar(void);
 
#include "stdio.h" 
main() {
char c;   
int letters=0,space=0,digit=0,others=0;  
printf("please input some characters\n");  
while((c=getchar())!=‘\n‘)  { 
 if(c>=‘a‘&&c<=‘z‘||c>=‘A‘&&c<=‘Z‘)   letters++;  else if(c==‘ ‘)   space++;     
else if(c>=‘0‘&&c<=‘9‘)        digit++;      
else         others++;
} 
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters, space,digit,others);
 }
 

getchar()解决“输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数”

原文:https://www.cnblogs.com/qq2806933146xiaobai/p/12230544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!