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个回答  2018-11-13
第10行改为if('a'<=nextchar&&nextchar<='z'||'A'<=nextchar&&nextchar<='Z')
14行改为else if('0'<=nextchar&&nextchar<='9')
不能用你那种形式来表示集合
第2个回答  2012-12-02
#include<stdio.h>
#define N 100
int main()
{
char a[N];
int i,m=0,n=0,b=0,c=0;
char *p;
printf("Input a string:");
gets(a);
p=a;
for(i=0;p[0]!='\0';i++,p++)
{
if(p[0]>='a'&&p[0]<='z'||p[0]>='A'&&p[0]<='Z')
m++;
else if(p[0]>='0'&&p[0]<='9')
n++;
else if(p[0]==' ')
b++;
else
c++;
}
printf("英文字母:%d\n",m);
printf("数字字符:%d\n",n);
printf("空格:%d\n",b);
printf("其他字符:%d\n",c);
return 0;
}
第3个回答  2016-06-23
#include<stdio.h>
#define N 100
int main()
{
char a[N];
int i,m=0,n=0,b=0,c=0;
printf("Input a string:");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
m++;
else if(a[i]>='0'&&a[i]<='9')
n++;
else if(a[i]==' ')
b++;
else
c++;
}
printf("英文字母:%d\n",m);
printf("数字字符:%d\n",n);
printf("空格:%d\n",b);
printf("其他字符:%d\n",c);
return 0;
}
第4个回答  2013-06-25

这样的结果满意吗

第5个回答  2018-01-15
str[i]是指数组的位置,将一个字符串转成char类型的字符数组,
然后,for循环遍历该数组的每一个字符。
str[i]是指数组的位置,i为前面for里面的一个自增变量。
str[i]=32,你可以查看acsii表,编号为32的就是空格。因为一行英文字母中有可能有空格字符。
char类型的数据,本身可以转成int型 。互转方式就是通过ascii表进行互转。
space自变量指的是空格字符。