湖畔读书人


私信TA

用户名:2814787590

访问量:120443

签 名:

等  级
排  名 22
经  验 18768
参赛次数 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 人评分

  评论区

我觉得要把整形c声明成char类型的
2019-05-26 18:59:44
#include<iostream>
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
int a=0,b=0,c=0,d=0,e;
cin>>e;
while((e=getchar())!='\n'){
if(e >= 'A' && e<='Z' || e >= 'a' && e <= 'z')
a++;
if(e >= '0' && e <= '9')
b++;
if(e==' ')
c++;
else
d++;
}
cout<<a<<b<<c<<d;
return 0;
}
为什么答案错误
2019-05-04 16:23:52
else if(c>='0' && c<='9')
可以问一下为什么数字不加单引号结果就有问题emmm(⊙o⊙)
2019-04-01 17:27:03
#include<stdio.h>
int main()
{
int a=0,b=0,c=0,d=0,i;//a表示英文字母b表示数字c表示空格d表示其他字符;
scanf("%d",&i);
while((i= getchar()) !='\n')
{
if(i>='A' && i<='Z' || i>='a' && i<='z')
a++;
else if(i>='0' && i<='9')
b++;
else if(i==' ')
c++;
else
d++;
}
printf("%d %d %d %d\n",a,b,c,d);
return 0;
}
运行可以,提交显示错误百分之五十
2019-03-29 00:46:54
#include<stdio.h>
int main()
{
	char a,j=0,k=0,i=0,l=0;		//j字母,k数字,i空格,l其他字符
	while(a=getchar())
	{
		if(a==10)
			break;
		else if(a>=65&&a<=122)
			j++;
		else if(a>=48&&a<=57)
			k++;
		else if(a==32)
			i++;
		else 
			l++;
	}
	printf("%d  %d  %d  %d\n",j,k,i,l);
	return 0;
}

答案错误,不知道哪里不对,求告知
2019-03-26 08:46:55
#include<stdio.h>
#include<conio.h>
int main() 
{
	int num = 0, letter = 0, space = 0, other = 0;
	char c;
		c=_getche( );
		while (c!='\r')
		{
			if ('0' <= c && c <= '9')
				num++;
			else if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z')
				letter++;
			else if (c == ' ')
				space++;
			else other++;
			c = _getche();
		}
		printf("\n%d %d %d %d", letter, num, space, other);
	
	return 0;
}

自己运行没有问题,提交提示编译错误,有没有大佬指导下,哪里有错误
2019-03-10 00:05:07
#include<stdio.h>
int main()
{
    int c=0, n=0, o=0, b=0, p;
    while((p=getchar())!='/n')
    {
        if(p>='a'&&p<='z'||p>='A'&&p<='Z')
        c++;
        else if(p>='0'||p<='9')
        n++;
        else if(p==' ')
        b++;
        else
        o++;
    }
    printf("%d %d %d %d", c, n, b, o);
    return 0;
}
为什么时间超限啊
2019-03-08 10:37:33
#include <stdio.h>
int main()
{
  /* 输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。 */
  int c;
  int nY,nS,nB,n,nQ=0;
  n=nY=nB=nS=nQ;
  while((c=getchar())!= '\n'){
    n++;
    if(c>='a'&&c<='z'||c>='A'&&c<='Z') nY++;
    if(c>='0'&&c<='9')  nS++;
    if(c==' ') nB++;
    nQ=n-nY-nS-nB;
  };
  printf("%d %d %d %d",nY,nS,nB,nQ);
  return 0;
}
2019-02-26 18:58:19