小甲鱼第35个视频第三个题目没有讲答案,求助大神
本帖最后由 董瑞枫 于 2022-1-3 15:32 编辑编写一个用来统计各个数字,空白符(空格,制表符,换行符)以及所有其他字符出现次数的程序。分别储存在num【10】,blank,others里面并打印出来。谢谢大哥! 所以你想连全部的空白符都计算?那么你的字符串从哪里开始算起,又从哪里结尾呢?(一般换行当作结尾,但是你连换行都要算) 从开始输入算起,输入EOF结尾,全部的空白符都算,输入CTRL+Z,然后回车截止 #include <stdio.h>
#include <ctype.h>
int main(){
char c;
int digit = 0, lower = 0, upper = 0, newline = 0, tab = 0, space = 0, other = 0;
while((c = getchar()) != EOF){
if(isdigit(c)) digit++;
else if(islower(c)) lower++;
else if(isupper(c)) upper++;
else if(c == '\n') newline++;
else if(c == '\t') tab++;
else if(c == ' ') space++;
else other++;
}
printf("digit = %d\nlower = %d\nupper = %d\nnewline = %d\ntab = %d\nspace = %d\nother = %d", digit, lower, upper, newline, tab, space, other);
return 0;
} 其它空白符可以自行加,如'\r'、'\f' 等等
页:
[1]