解题思路和注意事项:
在这里我们会用到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 人评分
#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; } 为什么答案错误
rinne 2019-05-04 16:52:27 |
已解决,getchar就是输入
else if(c>='0' && c<='9') 可以问一下为什么数字不加单引号结果就有问题emmm(⊙o⊙)
飞跃孤星 2019-04-10 17:22:26 |
c是char类型的,>,<比较的时候是比较ASCII码,数字加引号是字符(0,ASCII码为64,9为73),不加引号是指ASCII码为0和9所代表的字符,结果就不对了
#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; } 运行可以,提交显示错误百分之五十
simplelife 2019-03-29 00:47:09 |
求解答
simplelife 2019-03-29 00:58:14 |
已经解决
simplelife 2019-03-29 00:59:50 |
在用到i=getchar()让i得到一个值的时候,不能再用到scanf(“%d”,&i);输入i 的值
#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; } 答案错误,不知道哪里不对,求告知
simplelife 2019-03-29 00:44:14 |
大写字母65到90和小写字母97到122之间还有其它的字符
#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-13 14:54:39 |
while那里是不是把n写成r了...
Strider 2019-03-13 17:21:18 |
@15138964863 这里如果用\n是不能执行的 可以认为在getche中enter==\r,所以我在输出前边加了换行,不然输出和输入的字符会叠在一行。你可以运行下试试。
飞跃孤星 2019-04-10 17:46:16 |
首先我本地跑也没什么问题的,其次 #include<conio.h> 是比较老的MSDOS字符界面库,其中的_getche()函数,现在几乎已经淘汰了,所以可能是网页后台没有配置这么老的库,还是使用 <stdio.h>里的比较通用的getchar()吧
Strider 2019-04-10 23:47:57 |
好的,谢谢了
#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; } 为什么时间超限啊
#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; }
z能 2019-10-08 16:46:18 |
getchar的返回值是字符的ASCII码,是整型的,声明成整型没问题。