[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

相關文章
相關標籤/搜索