用C语言编写一段程序,要求:输入一段字符,分别统计出其中的英文字母、空格、数字和其他字符的个数。(

用C语言编写一段程序,要求:输入一段字符,分别统计出其中的英文字母、空格、数字和其他字符的个数。(要用到while和getchar。初学C语言,有很多语句还没学,请用简单的语句编写)

你好!

    

给你一个程序,你试试吧,有问题再问

#include <stdio.h>
#include <string.h>

main()
{
char c[20];
int i=0,j=0,k=0,l=0,h=0;
printf("请输入一个字符串:");
gets(c);
 
for(i=0;i<=strlen(c);i++)
{
if(c[i]>='0'&&c[i]<='9')
j++;

if((c[i]>='a'&&c[i]<='z') || (c[i]>='A'&&c[i]<='Z'))
k++;

if(c[i]==' ')
l++;

if((c[i]>32&&c[i]<=47)||(c[i]>=58&&c[i]<=64)||(c[i]>=91&&c[i]<=96)||(c[i]>=123&&c[i]<=126))h++;
}
printf("数字有%d个\n",j);
printf("字母有%d个\n",k);
printf("空格有%d个\n",l);
printf("其它字符有%d个\n",h);
return 0;
}

追问

这个程序好像可以,但是有些语句我都没学过,不能用。还是谢谢你~

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-05
/*#include<ctype.h>中有对字符判断的函数,可以判断是否是字母、数字、空格、字符等,你可以自己去查一查*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>

void main ()
{
char a[100];
int len;
int nchar=0,nspace=0,nnum=0,nother=0;//字母、空格、数字、其他字符的个数
gets(a);
len=strlen(a);
for(int i=0;i<len;i++)
{
if(isalpha(a[i]))
nchar++;
else if(isspace(a[i]))
nspace++;
else if(isdigit(a[i]))
nnum++;
else
nother++;
}
printf("字母个数:%d\n",nchar);
printf("空格个数:%d\n",nspace);
printf("数字个数:%d\n",nnum);
printf("其他个数:%d\n",nother);
getchar();
}
第2个回答  2013-11-05
#include<stdio.h>
void main()
{
int letter=0,number=0,blank=0,other=0;
char c; //用来读取每个字符
while ((c=getchar()())!='\n') //当读入的是回车即为结束运算
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
letter++;
else if(c>='0'&&c<='9')
number++;
else if(c==' ')
blank++;
else other++;
}
printf("letter=%d,number=%d,blank=%d,other=%d",letter,number,blank,other);
}
第3个回答  2013-11-05
用asc码区分。每次输入就判断一次
第4个回答  2013-11-05
#include <stdio.h>

int main()
{
int _a=0,_b=0,_c=0,_d=0;
char c;
c=getchar();
while(c!='\n')
{ if(c<='z'&&c>='a'||c<='Z'&&c>='A') _a++;
else if(c>47&&c<58) _b++;
else if(c=' ') _c++;
else _d++;
c=getchar();
}
printf("字母有“%d”个\n",_a);
printf("数字有“%d”个\n",_b);
printf("空格有“%d”个\n",_c);
printf("其他字符有“%d”个\n",_d);

return 0;
}