-->
当前位置:首页 > 题库 > 正文内容

程序填空题:统计各类字符个数

Luz4年前 (2021-05-10)题库3362
以下程序统计输入的一行字符中字母、数字、空格、其它字符的个数(行末以换行符结束,最后的换行符不统计在内)。例如,输入的一行字符为```aB 3*```则字母有2个,数字有1个,空格有1个,其它字符有1个。

提示:1、填写的代码要确保程序可以正确编译、运行得出结果;2、空格之后的括号里注明了分数,该括号不属于代码部分。

```c
#include
int main(){
int letters, digits, spaces, others;
@@[letters = digits = spaces = others = 0](2);
char ch;
while( @@[(ch=getchar())!= '\n'](2) ){
if( @@[(ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z')](2) )
letters++;
else if( @@[ch>='0' && ch<='9'](2) )
digits++;
else if( @@[ch == ' '](2) )
spaces++;
else
others++;
}
printf("字母、数字、空格、其它字符分别有:%d %d %d %d 个\n",letters,digits,spaces,others);
}
```





答案:
第1空:letters = digits = spaces = others = 0

第2空:(ch=getchar())!= '\n'

第3空:(ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z')

第4空:ch>='0' && ch<='9'

第5空:ch == ' '

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。