码迷,mamicode.com
首页 > 其他好文 > 详细

709. To Lower Case

时间:2020-05-16 10:32:54      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:put   input   tput   imp   cte   har   char   character   amp   

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"

class Solution {
    public String toLowerCase(String str) {
        int k = (int) (‘a‘);
        StringBuilder sb = new StringBuilder();
        for(char c: str.toCharArray()){
            int t = (int) (c);
            if(!Character.isLetter(c)) sb.append(c);
            else if(t >= k) sb.append(c);
            else sb.append((char) (t + 32));
        }
        return sb.toString();
    }
}
    public String toLowerCase(String str) {
        char[] a = str.toCharArray();
        for (int i = 0; i < a.length; i++)
            if (‘A‘ <= a[i] && a[i] <= ‘Z‘)
                a[i] = (char) (a[i] - ‘A‘ + ‘a‘);
        return new String(a);
    }

 

709. To Lower Case

标签:put   input   tput   imp   cte   har   char   character   amp   

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12898942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!