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"

解題思路

  1. 利用ASCII Table大寫轉小寫。

程式解答

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;
    }
};

Last updated

Was this helpful?