[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;
    }
};
posted @ 2018-09-05 21:23  April15  阅读(130)  评论(0编辑  收藏  举报