我的代码
#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大致作用一样,两者不要一起用。