c语言程序设计 输入一个字符,分别统计出其中英文字母,空格 数字 和其他字符的个数

如题所述

#include "stdio.h"
main()
{int i,j,k,m;char ch;
i=j=k=m=0;
scanf("%c",&ch);
if((ch>='a'&&ch<='z')||(ch>='A' && ch<='Z'))i++;
else if(ch==' ')j++;
else if(ch>='0' && ch<='9')k++;
else m++;
printf("字母的个数为:%d\n",i);
printf("空格的个数为:%d\n",j);
printf("数字的个数为:%d\n",k);
printf("其他符的个数为:%d\n",m);}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-12
[cpp] view plain copy
#include<stdio.h>
#include<string.h>
//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
int main()
{
int letter=0,space=0,number=0,other=0;
int i;
char s[100];
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')
letter++;
else if(s[i]==' ')
space++;
else if(s[i]<='9'&&s[i]>='0')
number++;
else
other++;
}
printf("%d\n%d\n%d\n%d\n",letter,space,number,other);
第2个回答  2013-11-24
编写提纲:
设置一串int型变量,用于存储各种个数,一个char 存储读入的字符
用get()读入,因为空格也要
用if()比较
例如:if(cha==" ")存储空格的数字加一
以此类推
输出各个类型的个数。。
例如:printf("空格的个数是%d\n",kongge);
相似回答