Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
将字符串的大写字母转成小写字母字符串。
Example 1:
Input: "Hello"
Output: "hello"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
思路: ASCII码中大写字母比小写字母小32,直接加上32变成小写字母。
class Solution {
public:
string toLowerCase(string str) {
for(char& c : str)
if(c >= 'A' && c<='Z') c += 32;
return str;
}
};
本文介绍了一个简单的C++函数实现,该函数可以将输入的字符串中的所有大写字母转换为小写。通过遍历字符串并利用ASCII码的特点,将每个大写字母对应的ASCII码值增加32来完成转换。
1447

被折叠的 条评论
为什么被折叠?



