LeetCode-To Lower Case

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24133491/article/details/82529268

Description:
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中的toLowerCase方法;这里需要注意的时所给字符串中的字符可能包含非字母,所以需要判断是否为大写字母,再进行转换;

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

猜你喜欢

转载自blog.csdn.net/qq_24133491/article/details/82529268
今日推荐