输入一行字母,分别统计其中的英文字母,空格,数字和其他字符的个数。遇到回车换行符结束、

C语言

#include <stdio.h>
void main()
{
char ch;
int digit = 0,letter= 0, other= 0, black = 0;
printf("Please enter a Line String:/n");
while ((ch = getchar()) != '\n') //输入一行字符,以回车换行符结束
{
if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) //字母判断条件
letter++;
else if(ch>='0' && ch<='9') //数字条件判断
digit++;
else if(ch==' ')
black++;
else
other++;
}
//输出结果
printf("字母:%d\n数字:%d\n空格:%d\n其他:%d\n",letter,digit,black,other);
}来自:求助得到的回答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-21
#include <stdio.h>
int main()
{
char a;
int number = 0;
int word = 0;
int nelse = 0;
int black = 0;
while ((a = getchar()) != '\n')
{
if ((a>='a' && a<='z') || (a>='A' && a<='Z'))
{
word++;
}
else if(a>='0' && a<='9')
{
number++;
}
else if(a==' ')
{
black++;
}
else
{
++nelse;
}
}
printf("字母:%d\n数字:%d\n空格:%d\n其他:%d\n",word,number,black,nelse);
}
第2个回答  2017-12-20
#include "stdio.h"
#include <math.h>
#include <stdlib.h>
int main()
{
int a=0,b=0,c=0,d=0;
char ch;
while((ch=getchar())!='\n')
{
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
a++;
else if(ch>='0'&&ch<'9')
b++;
else if(ch==' ')
c++;
else
d++;
}
printf("%d %d %d %d",a,b,c,d);
return 0;
}
相似回答