编程实现统计一行字符串中大小写字母,数字的个数

xiaoxiao2021-02-28  89

直接上代码

#include<strings.h> int main() { int big = 0; int small = 0; int space = 0; int number = 0; int others = 0; printf("please input a str:\n"); char a[50]; fgets(a,50,stdin); //stdin 意思是键盘输入 //gets从终端读入是的字符串是用\0结束的,而fgets是以\n结束的 //这里用gets出现空格字符串输入就会结束,导致无法统计空格 int i = 0; int len; len = strlen(a); for(i = 0;i<len;i++) { if((a[i] >= 'A') && (a[i] <= 'Z')) big++; else if((a[i] >= 'a') && (a[i] <= 'z')) small++; else if((a[i] >= '0') && (a[i] <= '9')) number++; else if(a[i] == ' ') space++; else others++; //结尾符\0算other } printf("big=%d\nsmall=%d\nspace=%d\nnumber=%d\nothers=%d\n",big,small,space,number,others); return 0; }

转载请注明原文地址: https://www.6miu.com/read-40624.html

最新回复(0)