Leetcode#709. To Lower Case(转换成小写字母)

题目描述

实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。

示例 1:

输入: "Hello"
输出: "hello"

示例 2:

输入: "here"
输出: "here"

示例 3:

输入: "LOVELY"
输出: "lovely"

思路

字符串转char数组,遍历数组,判断如果大写就转小写

代码实现

package String;

/**
 * 709. To Lower Case(转换成小写字母)
 * 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
 */
public class Solution709 {
    public static void main(String[] args) {
        Solution709 solution709 = new Solution709();
        String str = "Hello";
        System.out.println(solution709.toLowerCase(str));
    }

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

posted @ 2018-08-27 11:35  武培轩  阅读(1166)  评论(0编辑  收藏  举报