源一


私信TA

用户名:1016323

访问量:20585

签 名:

等  级
排  名 1296
经  验 2900
参赛次数 0
文章发表 5
年  龄 20
在职情况 学生
学  校 西安邮电大学
专  业 计算机科学与技术

  自我简介:

解题思路:    我们的想法是,定义变量分别存储数字,字母,空格和其他字符在字符串中出现的次数。当出现相应字符时,对应变量加一。 那么,我们就需要一个可以一个一个读入字符的函数,用来控制数据读入。每次计数都是一个重复的过程,那我们可以采用循环结构。

    这里介绍一个函数,getchar()函数用来读入字符,当遇到回车时,结束读入过程。我们可以用while循环和此函数完成此题。具体代码如下:



注意事项:1.要注意while循环控制条件中 (c=getchar())最外层括号不可省略,具体可参考C语言运算符优先级参考表。

              2.if选择结构中也可不采用ASC||码,可直接用对应字符作为判断条件

参考代码:

#include<stdio.h>
int main()
{
   int num=0,letter=0,space=0,other=0;
   int c;
   while((c=getchar())!='\n')
   {
       if(48<=c&&c<=57)
       num++;
       else if(65<=c&&c<=90||97<=c&&c<=122)
       letter++;
       else if(c==32)
       space++;
       else other++;
   }
   printf("%d %d %d %d",letter,num,space,other);
   return 0;
}


 

0.0分

26 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int ch;
	int alphabets,numbers,blanks,others = 0;

	while((ch = getchar()) != '\n')
	{
		if(ch >= 65 && ch <= 90||ch >= 97 && ch <= 122)
			alphabets++;
		else if(ch >= 48 && ch <= 57)
			numbers++;
		else if(ch == 32 )
			blanks++;
		else
			others++;
	}
	printf("%d %d %d %d", alphabets,numbers,blanks,others);

	return 0;
}
为什么numbers的结果不对?
2020-12-21 17:02:59
#include<stdio.h>
int main()

{
	int letter=0, number=0, blank=0, other=0,c;
	while ((c=getchar()) != '\n')
	{
		if (c >= 'a' && c <= 'z' or c >= 'A' && c <= 'Z')
			letter++;
		else if (c >= '0' && c <= '9')
			number++;
		else if (c == ' ')
			blank++;
		else
			other++;
	}
	printf("%d %d %d %d", letter, number, blank, other);
}
2020-04-08 12:19:19
可能是a、b、c、d的输出顺序与题目要求不同吧
2019-05-27 21:25:53
这个解法通俗易懂
2019-01-28 16:36:10
噗  我知道了, 写错了,尴尬
2018-05-30 19:34:45
#include<stdio.h>
int main(){
	char ch;
	int a=0,b=0,c=0,d=0;
	while( scanf("%c",&ch)!=EOF && ch!='\n'  ){
		if(ch==' '){
			c++;
		}else if(ch>='0' &&ch<='9'){
			b++;
		}else if( (ch>='a' && ch>='z') || (ch>='A' && ch>='Z')){
			a++;
		}else{
			d++;
		}
	}
	printf("%d %d %d %d\n",a,b,c,d);
	return 0;
}
请问为啥这样就提交不过去
2018-05-30 19:33:38
  • «
  • 1
  • »