CF59A Word(#模拟 -1.5)

题意翻译

给定一个字符串要求改变最少的字符使这个字符串中只有大写字母或小写字母,如果大写字符与小写字符个数一样,就全变为小写字符。

输入输出样例

输入样例#1

HoUse

输出样例#1

house

输入样例#2

ViP

输出样例#2

VIP

输入样例#3

maTRIx

输出样例#3

matrix

思路

普及一下姿势。

字符测试函数字符转换函数完成大小写字母的判断及变换。(需要包含头文件<ctype.h>

姿势点:

1.isupper() 判断是否是大写英文字母

2.islower() 判断是否是小写英文字母

3.tolower() 大写字母转小写

4.toupper() 小写字母转大写

OK,接下来纯模拟即可。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	char str[101]={};
	int i,a=0,b=0,len;
	cin>>str;
	len=strlen(str);//取长
	for(i=0;i<len;i++)
	{
		if(isupper(str[i]))//如果为大写
			a++;
		if(islower(str[i]))//如果为小写
			b++;
	}
	if(a>b)//如果大写字符比小写的多
	{
		for(i=0;i<len;i++)
		{
			if(islower(str[i]))
			str[i]=toupper(str[i]);
			cout<<str[i];
		}
	}
	else
	{
		for(i=0;i<len;i++)
		{
			if(isupper(str[i]))
			str[i]=tolower(str[i]);
			cout<<str[i];
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/apro1066/article/details/81115391
1.5
今日推荐