[LeetCode] To Lower Case 转为小写

 

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.html

 

Example 1:spa

Input: "Hello"
Output: "hello" 

Example 2:code

Input: "here"
Output: "here" 

Example 3:htm

Input: "LOVELY"
Output: "lovely"

 

这道题让咱们将单词转为小写,是一道比较简单的题目,咱们都知道小写字母比其对应的大写字母的ASCII码大32,因此咱们只须要遍历字符串,对于全部的大写字母,通通加上32便可,参见代码以下:blog

 

class Solution {
public:
    string toLowerCase(string str) {
        for (char &c : str) {
            if (c >= 'A' && c <= 'Z') c += 32;
        }
        return str;
    }
};

 

参考资料:leetcode

https://leetcode.com/problems/to-lower-case/字符串

 

LeetCode All in One 题目讲解汇总(持续更新中...)get

相关文章
相关标签/搜索