Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Input: "Hello"
Output: "hello"
Input: "here"
Output: "here"
Input: "LOVELY"
Output: "lovely"
public String toLowerCase(String str) {
if (str == null || str.length() == 0) {
return null;
}
// <uppercase, lowerCase>
Map<Character, Character> map = new HashMap<>();
for (char i = 0; i < 26; i++) {
char upperCase = (char)('A' + i);
char lowerCase = (char)('A' + i + 32);
map.put(upperCase, lowerCase);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char curChar = str.charAt(i);
if (map.containsKey(curChar)) {
curChar = map.get(curChar);
}
sb.append(curChar);
}
return sb.toString();
}
// 参考答案
public String toLowerCase(String str) {
Map<Character, Character> h = new HashMap();
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 26; ++i) {
h.put(upper.charAt(i), lower.charAt(i));
}
StringBuilder sb = new StringBuilder();
for (char x : str.toCharArray()) {
sb.append(h.containsKey(x) ? h.get(x) : x);
}
return sb.toString();
}