C++实现将输入字符串中的大写字母转为小写字母
- 编写一个函数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;
}
效果图

人生如逆旅
我亦是行人

浙公网安备 33010602011771号