709. To Lower Case

QUESTION:

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"

EXPLANATION:

其实str自己就有tolowcase的方法,但是如果直接写的话,反而失去了本来的意义,所以就写了这样。同时我们可以看一下str的tolowercase来看看差异点。

SOLUTION:

class Solution {
    public String toLowerCase(String str) {
        char[] chars = str.toCharArray();
        for(int i = 0;i<chars.length;i++){
            char tmp = chars[i];
            if(tmp<='Z' && tmp >='A')
                chars[i] = tmp+=32;
        }
        return new String(chars);
    }
}
>