程序如下:
#include <stdio.h>
void main()
{
char c;
int letter=0,space=0,digit=0,other=0;
printf("请输入一行字符:\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
else
if(c==' ')
space++;
else
if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("字母数=%d,空格数=%d,数字=%d,其他字符=%d\n",letter,space,digit,other);
}
求解while((c=getchar())!='\n')这行的作用?如果换成c=getchar(),那么程序只能统计字符串c第一个字符,为什么while((c=getchar())!='\n')能逐个判断统计字符?
用循环语句即可依次统计。
1、while语句:
2 ,do while语句: