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 @ 2024-12-09 23:57  相遇就是有缘  阅读(316)  评论(0)    收藏  举报