[LeetCode]709. To Lower Case

时间:2022-07-22
本文章向大家介绍[LeetCode]709. To Lower Case,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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;
    }
};