湖畔读书人


私信TA

用户名:2814787590

访问量:121240

签 名:

等  级
排  名 22
经  验 18876
参赛次数 0
文章发表 42
年  龄 0
在职情况 学生
学  校 武汉东湖学院
专  业

  自我简介:

解题思路和注意事项:


在这里我们会用到getchar()函数,简单来说,getchar()就是从键盘获取字符,直到回车为止;


代码中while里的表达式(c = getchar()) != '\n',意思是,当输入的字符不是换行符的时候,继续往下执行;


注意这里的括号不能丢,因为!=的优先级比=高,如果去掉了外面的括号就等价于 c = (getchar()!='\n');

参考代码:

#include<stdio.h>
int main()
{
	int letter = 0,number = 0,blank = 0,others = 0,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\n",letter,number,blank,others);
	return 0;
}


 

0.0分

176 人评分

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

编程语言转换

万能编程问答

代码解释器

  评论区

在c++2010都可以成功运行,为什么在这上面会出现时钟信号报错
#include<stdio.h>
int main()
{
   int E=0,shuzi=0,kongge=0,other=0;
   char ch;
   printf("请输入一字符串\n");
   while((ch=getchar())!='\n')
   {
       if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
       {
           E++;
       }
       else if(ch>='0'&&ch<='9')
       {
           shuzi++;
       }
       else if(ch==' ')
       {
           kongge++;
       }
       else 
       {
           other++;
       }
   }
    printf("%d %d %d %d",E,shuzi,kongge,other);
    return 0;
}
2022-01-24 21:45:37
为啥我一到读入空格就出问题
#include<stdio.h>
#define N 200
#include<string.h>
int main()
{
    char a[N];
    int i,E=0,shuzi=0,kongge=0,other=0;
    scanf("%s",a);
    int len=strlen(a);
    for(i=0;i<len;i++)
    {
        if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))
        {
            E++;
        }
        else if(a[i]>='0'&&a[i]<='9')
        {
            shuzi++;
        }
        else if(a[i]==' ')
        {
            kongge++;
        }
        else
        {
            other++;
        }
    }
    printf("%d %d %d %d",E,shuzi,kongge,other);
    return 0;
}
2022-01-24 21:15:44
#include<stdio.h>
int main()
{ 
     int n,h=0,j=0,l=0,y=0;//h为字母数,j为数字数,l为空格数,y为其他数。 
	while((n = getchar()) != '\n')
	{
	if(n>='A' && n<='Z'  || n>='a' && n<='z')
	h++;
	else if(n>='0' && n<='9')
	j++;
	else if(n==' ')
	l++;
	else
	y++;
}
printf("%d,%d,%d,%d",h,j,l,y);
return 0;
}
敢问各位大佬,我这里错在了哪里?
2021-12-07 19:54:41
为什么要用while
2021-12-01 20:38:09
a=getchar();
	while(a!='\n')

请问这是必须把a=getchar();写在while里面?
2021-11-14 10:52:17
while((c = getchar()) != '\n')   括号里为什么是“\n”呢?
2021-10-11 20:09:25
#include<stdio.h>
int main()
{
	int a,b,c,d,e;
	a=b=c=d=0;
	while((e=getchar())!='\n')	
	{
		if('a'<=e&&e<='z'||'A'<=e&&e<='Z')
		{
			a++;
		}
		if('0'<=e&&e<='9')
		{
			b++;
		}
		if(e==' ')
		{
			c++;
		}
		else
		{
			d++;
		}
	}
	printf("%d %d %d %d",a,b,c,d);
	getchar();
	return 0;
}
为什么我的就错了。。。
2021-09-17 18:41:44
为什么不能直接用三个if要用else if啊qwq
2020-10-06 17:13:52