mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Refactor and enhance the 'Upper' class (#6118)
This commit is contained in:
@ -21,15 +21,19 @@ public final class Upper {
|
|||||||
* @return the {@code String}, converted to uppercase.
|
* @return the {@code String}, converted to uppercase.
|
||||||
*/
|
*/
|
||||||
public static String toUpperCase(String s) {
|
public static String toUpperCase(String s) {
|
||||||
if (s == null || s.isEmpty()) {
|
if (s == null) {
|
||||||
|
throw new IllegalArgumentException("Input string connot be null");
|
||||||
|
}
|
||||||
|
if (s.isEmpty()) {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
char[] values = s.toCharArray();
|
StringBuilder result = new StringBuilder(s);
|
||||||
for (int i = 0; i < values.length; ++i) {
|
for (int i = 0; i < result.length(); ++i) {
|
||||||
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) {
|
char currentChar = result.charAt(i);
|
||||||
values[i] = Character.toUpperCase(values[i]);
|
if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
|
||||||
|
result.setCharAt(i, Character.toUpperCase(currentChar));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new String(values);
|
return result.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user