C++实现将输入字符串中的大写字母转为小写字母

  1. 编写一个函数to_lower(),实现将字符串中的大写字母转换成相应的小写字母,在程序的主函数中输入数据并输出结果。

答案一

#include<iostream>
using namespace std;

void to_lower(char a[])
{
	for (int i = 0; i < 10 && a[i] != '\0'; i++) {
		if (a[i] >= 'A' && a[i] <= 'Z') {
			a[i] += 32;
		}
	}
}

void main() {
	char str[10];
	cin >> str;
	to_lower(str);
	cout << str << endl;
	return;
}

答案二

#include<iostream>
using namespace std;

void to_lower(char a[])
{
	int arrSize = sizeof(a) / sizeof(a[0]);
	for (int i = 0; i<10 && i < arrSize; i++)
	{
		if (a[i] >= 'A' && a[i] <= 'Z') {
			a[i] = a[i] + 32;
		}
	}
}

void main() {
	char str[10];
	cin >> str;
	to_lower(str);
	cout << str << endl;
	return;
}

效果图

posted @   相遇就是有缘  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
  1. 1 我记得 赵雷
  2. 2 北京东路的日子 汪源
  3. 3 把回忆拼好给你 王贰浪
北京东路的日子 - 汪源
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Not available

点击右上角即可分享
微信分享提示