[LeetCode] 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;
    }
};

 

參考資料:

https://leetcode.com/problems/to-lower-case/

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM