输入一些字符,统计数字字符、字母字符及其它字符在字符串中所占的百分比。提示:请使用ctype.h头文件中定义的字符分类函数。
3条回答 默认 最新
xiexiexiexieqing 2021-06-13 00:02关注#include <stdio.h> #include <stdlib.h> #include <ctype.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int a,b,c,i,n; char op; //字符串 printf("输入字节长度:"); scanf("%d",&n); for(i=1;i<=n;i++) { op=getchar(); //输入字符串 if(op>='A'&&op<='Z'||op>='a'&&op<='z') a++; //满足字母条件 变量+1 else if(op >='0'&&op<='9') b++; //满足数字条件 变量+1 else c++; } printf("字母%d 数字%d 其他%d",a,b,c); printf("\n"); float A,B,C,N; A=a,B=b,C=c,N=n; printf("字母:%.1f%% 数字:%.1f%% 其他:%.1f%% ",A/N*100,B/N*100,C/N*100); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报