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

我自己编写了一个,可是出不来,还请大哥哥大姐姐帮帮我,谢谢。

非要限制输入的大小么?过会儿给你发个
#include <stdio.h>
#include <string>
void main(){
char m_input;
int digit=0,space=0,others=0,uppercase=0,lowercase=0;
printf("Please input string:");
while ((m_input=getchar())!='\n')
{
if (m_input>='a'&&m_input<='z')
lowercase++;
else if (m_input>='A'&&m_input<='Z')
uppercase++;
else if (m_input>'0'&&m_input<'9')
digit++;
else if (m_input==' ')
space++;
else
others++;
}
printf("lowercase:%d\t uppercase:%d\t digit:%d\t space:%d\t others:%d\n",lowercase,uppercase,digit,space,others);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-12-30
为了提高编程的能力,我给你提供的答案为输出三行的字符(你可以改为输入一行的)!
#include <stdio.h>

void main()
{
int i,j,upp,low,dig,spa,oth;
char text[3][80];
upp=low=dig=spa=oth=0;

for (i=0;i<3;i++)
{
printf("please input line %d:\n",i+1);
gets(text[i]);
for (j=0;j<80 && text[i][j]!='\0';j++)
{
if (text[i][j]>='A'&& text[i][j]<='Z')
upp++;
else if (text[i][j]>='a' && text[i][j]<='z')
low++;
else if (text[i][j]>='0' && text[i][j]<='9')
dig++;
else if (text[i][j]==' ')
spa++;
else
oth++;
}
}

printf("\nupper case: %d\n",upp);
printf("lower case: %d\n",low);
printf("digit : %d\n",dig);
printf("space : %d\n",spa);
printf("other : %d\n",oth);
}
第2个回答  2009-12-30
void main()
{int i,s=0,b=0,c=0,d=0,e=0;
char a[50];
gets(a);
for(i=0;i<50;i++)
{
if(a[i]>='A'&&a[i]<='Z')s++;
else
if(a[i]>='a'&&a[i]<='z')b++;
else
if(a[i]>=1&&a[i]<=9)c++;
else
if(a[i]=='')d++;
else e++;}
printf("A-Z:%d a-z:%d 1-9:%d space:%d others:%d ",s,b,c,d,e);
}
相似回答