C语言题目输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。

#include <stdio.h>
int main()
{
int letter=0,space=0,number=0,others=0;
char nextchar;
printf("Input your string\n");
for(;nextchar!='\n';)
{
scanf("%c",&nextchar);
if('a'<=nextchar<='z'||'A'<=nextchar<='Z')
letter++;
else if(nextchar==' ')
space++;
else if('0'<=nextchar<='9')
number++;
else
others++;
}
printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,others);
}

哪错啦

第1个回答  2010-03-30
#include <stdio.h>
#include <string.h>
#define A 80
main()
{
char str[A];
int len,i,letter=0,digit=0,space=0,others=0;
printf("请输入一行字符:");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
letter++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else
others++;
}
printf("英文字符有:%d\n",letter);
printf("数字字符有:%d\n",digit);
printf("空格有:%d\n",space);
printf("其他字符有:%d\n",others);
}
第2个回答  2015-07-26
#include <stdio.h>
int main()
{
int a=0,b=0,c=0,d=0;
char e;
printf("请输入一段字符:\n");
while((e=getchar())!='\n') { //注意优先级,加括号,删分号
     if(e==' ')//用a来接受空格的个数 
     {
     a++; 
     }
     if(e>='0' && e<='9')// 数字是'0'到'9'的字符,不是ASCII值0到9
     {
     b++;
     }
     if((e>=65&&e<=90)||(e>=97&&e<=122))//用c来接受字母的个数 
     {
     c++;
     }
     else //用d来接受其他字符的个数
     {
     d++;
     }
    }
    printf("共输入空格%d个\n",a);
    printf("共输入数字%d个\n",b);
    printf("共输入字母%d个\n",c);
    printf("共输入其他字符%d个\n",d);
    return 0; 
}

第3个回答  2010-04-01
用for语句编的.....

#include<stdio.h>
void main()
{
int z,k,s,q;
char ch;
z=k=s=q=0;
for(ch=getchar();ch!='\n';;)
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
z++;
else if(ch==' ')
k++;
else if(ch>='0'&&ch<='9')
s++;
else q++;
ch=getchar();
}
printf("zimu:%d\nspace:%d\nshuzi:%d\nqita:%d\n"z,k,s,q);
}
第4个回答  2018-11-30
#include<stdio.h>
int main()
{
char c;
int letters=0,space=0,digit=0,other=0;
printf("请输入一段字符\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其他字符数:%d\n",letters,space,digit,other);
return 0;
}
如果没有问题请采纳 谢谢
第5个回答  2010-05-18
#include<stdio.h>
void main()
{
char a[50];
int letter=0,number=0,blank=0,other=0,i=0;
gets(a);
while(a[i]!='/0')
{
if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))
letter++;
else if(a[i]>='0'&&a[i]<='9')
number++;
else if(a[i]==' ')
blank++;
else other++;
i++;
}
printf("letter=%d,number=%d,blank=%d,other=%d",letter,number,blank,other);
}
在输入字符串时,记着在输入结束位置输入'/0'即可。