版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38088298/article/details/81586049
- 描述:
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
Input: “Hello”
Output: “hello”
Example 2:
Example 2:
Input: “here”
Output: “here”
Example 3:
Example 1:
Input: “LOVELY”
Output: “lovely”
- 分析:给定一个字符串,要求将字符串中的大写字母转换为小写字母,水题,只需知道一点大小写字母之间相差32,即
'a' - 'A' = 32
。 - 思路一:c++遍历进行转化。
class Solution {
public:
string toLowerCase(string str) {
for (int i = 0; i < str.length(); i++) {
if (str[i] >= 'A' && str[i] <= 'Z') str[i] = str[i] + 'a' - 'A';
}
return str;
}
};