湖畔读书人


私信TA

用户名:2814787590

访问量:121470

签 名:

等  级
排  名 22
经  验 18906
参赛次数 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编程助手,随时响应你的问题:

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

代码解释器

代码纠错

SQL生成与解释

  评论区

不知道还要考虑什么
#include<stdio.h>
#include<string.h>
void main()
{char a[200];
 int i=0,x=0,y=0,z=0,s=0;
gets(a);
while(a[i]!='\0')
{
	if('A'<=a[i]&&'z'>=a[i])
	{x+=1;i++;}
else if('0'<=a[i]&&'9'>=a[i])
{y+=1;i++;}
else if(a[i]==' ')
{z+=1;i++;}
else 
{s+=1;i++;}
}

    printf("%d %d %d %d\n",x,y,z,s);
}
2023-02-15 13:39:11
想问下uu们这个为什么是错的呀
#include <stdio.h>
int main()
{
	char ch[200];
	int i,a=0,b=0,c=0,d=0;
	gets(ch);
	for(i=0;ch[i]!='\0';i++)
	{
		if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))  a++;
		if(ch[i]>='0'&&ch[i]<='9') b++;
		if(ch[i]==' ') c++;
		else d++;
	}
	printf("%d %d %d %d",a,b,c,d);
	return 0;
}
2022-12-07 15:45:20
请问哪里错了
#include<stdio.h>
int main()
{
    char a[10000];
    int i=0,j;
    int space=0,num=0,letter=0,others=0;
    while(~scanf("%c",&a[i])) i++;
    for(j=0;j<i;j++)
    {
        if(a[j]==32)space++;
        else if(a[j]>=48&&a[j]<=57) num++;
        else if(a[j]>=65&&a[j]<=90) letter++;
        else if(a[j]>=97&&a[j]<=122) letter++;
        else others++;
    }
    printf("%d %d %d %d",letter,num,space,others);
    return 0;
}
2022-09-22 15:14:54
while((c = getchar()) != '\n')
里面那个怎么==\n呢,又没输入\n,那个\n怎么来
2022-06-15 01:59:25
请问哪里错了
#include<stdio.h>
int main()
{
    int a=0,b=0,c=0,d,e;
    while(e!='\n'){
        e=getchar();
        if( e >= 'A' && e <= 'Z' || e >= 'a' && e <= 'z' ){
            a++;
        }else if( e >= '0' && e <= '9' ){
            b++;
        }else if( e == ' ' ){
            c++;
        }else{
            d++;
        }
    }
	printf("%d %d %d %d\n",a,b,c,d);
	return 0;
}
2022-06-06 16:29:34
while ((data = getchar()) != '\n')
    {
        if(data >= 0x41 && data<= 0x5A || data >= 0x61 && data <= 0x7A)    
        {
            letterCounts++;
        }
        else if(data >= 0x30 && data <= 0x39) 
        {                    
            numberCounts++;
        }
        else if(data == 0x20) 
        {                               
            spaceCounts++;
        }
        else  
        {                                           
            otherCounts++;
        }
    }
    printf("%d %d %d %d\n" ,letterCounts ,numberCounts ,spaceCounts ,otherCounts);
2022-05-15 18:29:52
#include<stdio.h>
int main(){
	int letter=0,number=0,blank=0,other=0,c;
	while(c!='\n'){
		c = getchar();
		if(c>='a' && c<='z' || c>='A' && c<='Z'){
			letter++;
		} else if(c>='0' && c<='9'){
			number++;
		} else if(c == ' '){
			blank++;
		} else {
			other++;
		}
	}
	printf("%d %d %d %d\n",letter,number,blank,other-1);
}
2022-04-22 12:36:36
为什么这个getchar()必须写到while里面?像下面这样写到外面不行?
int main()
{
    int c1=0,c2=0,c3=0,c4=0,array;
    array=getchar();
    while(array!='\n'){
        if(array>='A'&&array<='Z'||array>='a'&&array<='z')  c1++;
        else if(array>='0'&&array<='9') c2++;
        else if(array==' ') c3++;
        else c4++;
    }
    printf("%d %d %d %d",c1,c2,c3,c4);
    return 0;
}
2022-03-30 20:23:53