題目描述
實現函數 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);
}
}