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个回答  2016-05-12
#include<stdio.h>
int main(void)
{
    //输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
    char ch;
    int char_num=0,kongge_num=0,int_num=0,other_num=0;
    while((ch=getchar())!='\n')//回车键结束输入,并且回车符不计入
    {
        if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
        {
            char_num++;
        }
        else if(ch==' ')
        {
            kongge_num++;
        }
        else if(ch>='0'&&ch<='9')
        {
            int_num++;
        }
        else
        {
            other_num++;
        }
    }
    printf("字母= %d,空格= %d,数字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
    return 0;
}

第2个回答  2013-11-01
只要做一个循环,求每一个字符的ASCII码,然后进行比较便可以得出其的总数
char *str1[]="abcde 123456 6/\{}"
int a[4]={0};
char temp_data;
unsigned char temp_data;
int i;
for(i=0;i<strlen(str1);i++)
{
temp_data=str1[i];
if(temp_data>=0x30 &&temp_data<=39) a[0]++;
else
if((temp_data>=0x41 &&temp_data<=5a)||(temp_data>=0x61 &&temp_data<=7a)) a[1]++;
else
if(temp_data==0x20) a[2]++;
else a[3]==;
}
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
第3个回答  2010-05-18
把字符串里的所有字符先定义成特殊字符,输入15个以后,检查剩下的35个是否为特殊字符,来判断其是否使用
第4个回答  2016-12-27
#include<stdio.h>
#include<string.h>
int main() 
{
char c[10000];
gets(c);
int i,zm=0,sz=0,kg=0;
for(i=0;c[i];i++){
if((c[i]<='z'&&c[i]>='a')||(c[i]<='Z'&&c[i]>='A'))zm++;
if(c[i]<='9'&&c[i]>='0')sz++;
if(c[i]==' ')kg++;
}
int qt=strlen(c)-zm-sz-kg;
printf("字母为%d 空格为%d 数字为%d 其它为%d\n",zm,kg,sz,qt);
return 0; 
}

望采纳,不懂可追问.

第5个回答  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);
}