problem 1021

我的代码

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

AC

#include<stdio.h>
int main()
{
	int letter=0,number=0,blank=0,others=0;
	char c;
	while((c=getchar())!='\n')
	{
			
	if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
		{
			letter++;
		}
	else if(c>='0'&&c<='9')
		{
			number++;
		}
	else if(c==' ')
		{
		blank++;
		}
	else {
		others++;
	}
	}
	printf("%d %d %d %d",letter,number,blank,others);
	return 0;
}

反思
对getchar的用法认识不清。
我的代码:

  • scanf("%s",&i);
  • while((i=getchar())!=’\n’)
    AC:
  • while((c=getchar())!=’\n’)

getchar()的用法

      字符数据的输入:
      scanf()函数是格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量中。
      在scanf()语句的格式串中由于没有非格式字符在“%d%d%d”之间作为输入时的间隔,因此在输入时要用一个以上的空格或回车符作为每两个输入数之间的间隔。scanf()在读取数字时会跳过空格、制表符和换行符
      getchar()函数是键盘输入函数,其功能是从键盘上输入一个字符。
      简单来说就是getchar()函数在C程序中的功能是接收一个字符。
      scanf和getchar大致作用一样,两者不要一起用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值