帮C语言初学者改改错

来源:百度知道 编辑:UC知道 时间:2024/05/25 07:35:56
输入10个字符,统计其英文字母、空格或回车、数字字符和其他字符的个数。
#include<stdio.h>
void main(){
char c;
int letter=0,space=0,digit=0,other=0;
printf("请输入10个字符:\n");
while((c=getchar))!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("letter=%d,space=%d,digit=%d,other=%d\n",letter,space,digit,other);
}

char c[11]
scanf("%s",c);
或for(i=0;i<10;i++)
scanf("%c",c[i]);

再来输入,否者你那样输入的话,结果只有一个字符保存到c里面了。
还有就是楼上说的 getchar () 丢了个括号
#include<stdio.h>
void main(){
char c[11];
int letter=0,space=0,digit=0,other=0;
int i;
printf("请输入10个字符:\n");
for(i=0;i<10;i++)
scanf("%c",&c[i]);
for(i=0;i<10;i++)
{
if(c[i]>='a'&&c[i]<='z'||c[i]>='A'&&c[i]<='Z')
letter++;
else if(c[i]>='0'&&c[i]<='9')
digit++;
else if(c[i]==' ')
space++;
else
other++;
}
printf("letter=%d,digit=%d,space=%d,other=%d\n",letter,digit,space,other);
}
改成这样。

#include<stdio.h>
void main(){
char c[20];
int letter=0,space=0,digit=0,other=0,i=0;
printf("请输入10个字符:\n");