作业 3 应用分支与循环结构解决问题 统计字符个数


/*统计字符,包括空格或回车,数字字符和其他字符*/
#include<stdio.h> int main(void) { int digit,space,letter,other; /*定义4个变量分别存放统计结果*/
char ch; int i; digit=space=letter=other=0; /*置放统计结果的4个变量的初值为零*/ printf("Enter 15 characters:"); /*输入提示*/
for(i=1;i<=15;i++){ /*循环执行了15次*/
ch=getchar(); /*输入一个字符*/
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) /*判断是否为字符*/
letter++; else if(ch>='0'&&ch<='9') /*判断是否为数字*/
digit++; else if(ch>=' '&&ch<=' ') /*判断是否为空格*/
space ++; else other++; } printf("letter=%d,digit=%d,space=%d,other=%d ",letter,digit,space,other); return 0; }
原文地址:https://www.cnblogs.com/huangsilinlana/p/3379093.html