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个回答  2006-04-06
int main()
{
int charcount = 0, chinesecount = 0, numcount = 0, spacecount = 0, othercount = 0, totalcount;
cout<<"请输入一行字符:";
char pstr[] = "1 a ,,,我 朥你", curchar;
// cin>>pstr;
curchar = pstr[0];
int curindex = 1;
while(curchar != '\0')
{
if (curchar >= 65 && curchar <= 122) //A-z
charcount++;
else if (curchar >= 128 || curchar < 0) //中文
chinesecount++, curindex++; //中文字符占两个字节,因此将游标向后移一位
else if (curchar >= 48 && curchar <= 57) //0-9
numcount++;
else if (curchar == 32) //空格
spacecount++;
else
othercount++;
curchar = pstr[curindex++];
}

totalcount = charcount + chinesecount + numcount + spacecount + othercount;
cout<<"总数:"<<totalcount<<" 英文字符:"<<charcount<<" 中文字符"<<chinesecount
<<" 数字:"<<numcount<<" 空格:"<<spacecount<<" 其它:"<<othercount;
}
第2个回答  2010-11-16
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int i,a[4];
char t;
int main()
{
for(i=0;;i++)
{
scanf("%c",&t);
if(t=='\n')break;
else if(t>='A'&&t<='Z') a[0]++;
else if(t>='a'&&t<='z')a[0]++;
else if(t==' ')a[1]++;
else if(t>='0'&&t<='9')a[2]++;
else a[3]++;

}
for(i=0;i<4;i++) printf("%d ",a[i]);
system("pause");
return 0;
}
输出分别是 字母 空格 数字 其他
看看行不行。
第3个回答  2014-04-11
//C语言代码:
#include <stdio.h>
int main()
{
    char c;
    int letters = 0, space = 0, digit = 0,others = 0,words = 0;
    bool flag;
    printf("请输入一行字符:");
    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
            others++;
        if( c == ' ')
            flag = false;
        else if(flag == false)
        {
            flag = true;
            words++;
        }
    }
    printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符:%d\n单词数:%d\n",letters,space,digit,others,words);
}

第4个回答  2014-05-27
#include<stdio.h>
#include<conio.h>
int main()
{
int a=0,b=0,c=0,d=0;
char e;
printf("输入一串字符(回车结束):\n");
while((e=getch())!=13)
{
printf("%c",e);
if((e>='a'&&e<='z')||(e>='A'&&e<='Z'))
a++;
else if(e>='0'&&e<='9')
b++;
else if(e==' ')
c++;
else
d++;
}
printf("\n英文字母:%d个,数字: %d个,空格:%d个,其他:%d个\n",a,b,c,d);
return 0;
}
第5个回答  2010-05-18
#include<stdio.h>
void main()
{
char a[50];
int letter=0,number=0,blank=0,other=0,i;
gets(a);
for(i=0;i<50;i++)
{
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++;
}
printf("letter=%d,number=%d,blank=%d,other=%d",letter,number,blank,other);
}
我默认输入的字符串是小于50个字符的,问题出在其他字符(other)这里,比如我输入了15个英文字母,他就默认a这个数组里面剩下的35个为其他字符了,有什么方法可以解决么?其他的都是正确的,就这里出了问题。。。