题解 CF59A 【Word】

· · 题解

题解 CF59A 【Word】

看到之前的几篇题解都是使用ASCII进行大小写字母的判断及变换的,其实也可以用字符测试函数字符转换函数完成大小写字母的判断及变换。(需要包含头文件<ctype.h>

字符测试函数

字符转换函数

Code:

#include<cstdio>
#include<cstring>
#include<ctype.h>//字符测试函数、字符转换函数头文件 
char ch[110];//字符数组 
int count_up=0, count_lo=0;//count_up->大写字母数  count_lo->小写字母数 
int main() {
    scanf("%s", ch);//输入字符串 
    int len=strlen(ch);//测出字符串长度 
    for(int i=0; i<len; i++) {//扫描字符串 
        if(isupper(ch[i])) count_up++;//计算大写字母数 
        if(islower(ch[i])) count_lo++;//计算小写字母数 
    }
    if(count_up>count_lo) {//如果大写字母多于小写字母 
        for(int i=0; i<len; i++)
            if(islower(ch[i])) ch[i]=toupper(ch[i]);//将小写字母转为大写字母 
    } else if(count_up<=count_lo)//如果大写字母少于或等于小写字母 
        for(int i=0; i<len; i++)
            if(isupper(ch[i])) ch[i]=tolower(ch[i]);//将大写字母转为小写字母 
    for(int i=0; i<len; i++) printf("%c", ch[i]);//输出 
    return 0;
}