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个回答  2020-03-17

第2个回答  2020-03-24
第3个回答  2019-06-21
1 getchar()
#include <stdio.h>
int main()
{
int c=0,s=0,n=0, o=0;
char i;
while((i=getchar())!='\n')
{
if((i>='A'&&i<='Z') || (i>='a'&&i<='z') )
c++;
else if(i>='0'&&i<='9')
n++;
else if(i == ' ')
s++;
else o++;
}
printf("英文字母 %d,空格 %d,数字 %d, 其他字符 %d\n", c, s, n, o);
return 0;
}
2 数组str[] 和gets(str)
# include <stdio.h>
int main(void)
{
int a[4]={0},i=0;
char str[80];
printf("请输入一行字符:\n");
gets(str);
while (str[i] != 0)
{
if (str[i]>='a' && str[i]<='z' || (str[i]>='A' && str[i]<='Z'))
a[0]++;
else if (str[i]>='0' && str[i]<='9')
a[1]++;
else if (str[i] == ' ')
a[2]++;
else
a[3]++;
i++;
}
printf("字母:%d\n数字:%d\n空格:%d\n其她:%d\n",a[0],a[1],a[2],a[3]);
return 0;
}
3 数组str[],gets(str)和指针p
#include<stdio.h>
int main()
{
int a=0,b=0,c=0,d=0,e=0;
char *p,str[80];
p=str;
gets(str);
while(*p)
if(*p>='A' && *p <='Z')
{a++;p++;}
else if(*p>='a' && *p <='z')
{b++;p++;}
else if(*p==' ')
{c++;p++;}
else if(*p>='0' && *p <='9')
{d++;p++;}
else
{e++;p++;}
printf("大写%d 小写%d 空格%d 数字%d 其它%d 英文%d\n",a,b,c,d,e,a+b);
return 0;
}
第4个回答  2011-04-18
语法错误:
printf("其中大写字母%d个,小写字母%d个,数字%d个,其他字符%d个\n",dx,xx,shuzi,qita);
dx后面的逗号不是英文的。算法也有错误:你判断的时候if(all[i]>'a'&&all[i]<'z'||all[i]>'A'&&all[i]<'Z')
应该把>都改成>=,<也一样,不改的话a、A、z、Z的判断将被划在其他类里,数字的判断也应该是大于等于,小于等于,改完后代码为:
#include <stdio.h>
#define N 100
main()
{
char all[N];
int i,xx=0,shuzi=0,qita=0,dx=0;
printf("请输入一个字符串(不超过100个):");
gets(all);
for(i=0;all[i];i++)
{
if(all[i]>='a'&&all[i]<='z'||all[i]>='A'&&all[i]<='Z')
if(all[i]>='A'&&all[i]<='Z')
dx++;
else
xx++;
else
if(all[i]>='0'&&all[i]<='9')
shuzi++;
else
qita++;
}
printf("其中大写字母%d个,小写字母%d个,数字%d个,其他字符%d个\n",dx,xx,shuzi,qita);
}