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-05-28
程序如下所示,仅供参考:
#include<stdio.h>
void hlw(char *s){
int zimu=0,shuzi=0,kongge=0,qita=0;
while(*s){
if(*s>='a'&&*s<='z'||*s>='A'&&*s<='Z')
zimu++;
else if(*s>='0'&&*s<='9')
shuzi++;
else if(*s==' ')
kongge++;
else
qita++;
s++;
}
printf("\n\n输入的字符串中\n\n字母个数为:%d\n数字个数为:%d\n空格个数为:%d\n其他的字符个数为:%d\n\n",zimu,shuzi,kongge,qita);
}
void main(){
char str[1000];
printf("请输入字符串:\n");
gets(str);
hlw(str);
}

希望对你有帮助,呵呵!
第2个回答  2016-05-12
#include<stdio.h>

int main(){
int ch, word, number, space, other;
word = number = space = other = 0;
while((ch = getchar()) != EOF){
if(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')){
//如果是字符
word++;
}else if('0' <= ch && ch <= '9'){
//如果是数字
number++;
}else if(ch == ' '){
//如果是空格
space++;
}else{
//其他
other++;
}
}
printf("word=%d\nnumber=%d\nspace=%d\nother=%d\n", word, number, space, other);
return 0;
}

第3个回答  2013-04-25
#include<stdio.h>
#include<string.h>
int main()
{
char s[100]="The furthest distance in the world,1942 is not between life and death.";
int n,nc=0,ns=0,nm=0,no=0;
//nc,ns,nm,no分别表示中英文字符个数,空格个数,数字个数,其他字符个数
int i;
n=strlen(s); //n为字符串长度
for(i=0;i<n;i++)
if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
nc++;
else if(s[i]==32)
ns++;
else if(s[i]>=48&&s[i]<=57)
nm++;
else
no++;
printf("中英文字符数为:%d\n",nc);
printf("空格数为:%d\n",ns);
printf("数字个数为:%d\n",nm);
printf("其他字符数为:%d\n",no);
return 0;
}
第4个回答  2010-07-26
public class Test{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
String str=scan.next();
System.out.println(str.length());
}
}
第5个回答  2013-04-25
#include<stdio.h>
void add(int *a,int *b,int *c,int *d)
{

for (i = 0; i < s.Length; i++)
{
if (s[i] >= '0'&& s[i] <='9')
*a++;
else if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
*b++;
else if(s[i]==' ')
*c++;
else
*d++;
}
}
main()
{
int i,j=0;
int a=0,b=0,c=0,d=0;
string s;
printf("请输入一个字符串:");
while(s[j]!='\n')
{
scanf("%s",&s[j]);
j++;
}
add(&a,&b,&c,&d);
printf("数字的个数是:%d,字母的个数是:%d,空格的个数是:%d,其它字符的个数是:%d", a,b,c,d);
}