第1个回答 2019-09-06
1
while语句:
#include<stdio.h>
int
main(void)
{
//输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
char
ch;
int
char_num=0,kongge_num=0,int_num=0,other_num=0;
while((ch=getchar())!='\n')//回车键结束输入,并且回车符不计入
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else
if(ch=='
')
{
kongge_num++;
}
else
if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
}
printf("字母=
%d,空格=
%d,数字=
%d,其它=
%d\n",char_num,kongge_num,int_num,other_num);
return
0;
}
2
,do
while语句:
#include<stdio.h>
int
main(void)
{
//输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
char
ch;
int
char_num=0,kongge_num=0,int_num=0,other_num=0;
do
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else
if(ch=='
')
{
kongge_num++;
}
else
if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
}
while((ch=getchar())!='\n')//回车键结束输入,并且回车符不计入
printf("字母=
%d,空格=
%d,数字=
%d,其它=
%d\n",char_num,kongge_num,int_num,other_num);
return
0;
}
第4个回答 2020-06-22
int main()
{
char a[50];
int letter=0,number=0,blank=0,other=0;
int i;
gets(a);
char c;
for(i=0;(c=a[i])!='\0';i++)
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))letter++;
else if(c>='o'&&c<='9')number++;
else if(c==' ')blank++;
else other++;
}
printf("letter=%d,number=%d,blank=%d,other=%d\n",letter,number,blank,other);
return 0;
}