1. 程式人生 > >【LeetCode】709. To Lower Case題解(C++)

【LeetCode】709. To Lower Case題解(C++)

【LeetCode】709. To Lower Case(C++)

709. To Lower Case

Describe: 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"

思路解析: 對輸入string中的每一個字元進行依次判斷,若為大寫,轉換為小寫即可,若為小寫則無需做出任何變動。 AC程式碼(C++)

class Solution {
public:
    string toLowerCase(string str) {
        for(int i=0;i<str.length();i++){
            if(str[i]>='A'&&str[i]<='Z')
                str[i]+=32;
        }
        return str;
    }
};