1. 程式人生 > >[LeetCode]709. To Lower Case

[LeetCode]709. To Lower Case

out parameter imp () inpu love ++ strong UNC

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

[LeetCode]709. To Lower Case