Skip to content

Commit bd785de

Browse files
authored
Refactor and enhance the 'Upper' class (#6118)
1 parent 08c0f4a commit bd785de

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Diff for: src/main/java/com/thealgorithms/strings/Upper.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public static void main(String[] args) {
2121
* @return the {@code String}, converted to uppercase.
2222
*/
2323
public static String toUpperCase(String s) {
24-
if (s == null || s.isEmpty()) {
24+
if (s == null) {
25+
throw new IllegalArgumentException("Input string connot be null");
26+
}
27+
if (s.isEmpty()) {
2528
return s;
2629
}
27-
char[] values = s.toCharArray();
28-
for (int i = 0; i < values.length; ++i) {
29-
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) {
30-
values[i] = Character.toUpperCase(values[i]);
30+
StringBuilder result = new StringBuilder(s);
31+
for (int i = 0; i < result.length(); ++i) {
32+
char currentChar = result.charAt(i);
33+
if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
34+
result.setCharAt(i, Character.toUpperCase(currentChar));
3135
}
3236
}
33-
return new String(values);
37+
return result.toString();
3438
}
3539
}

0 commit comments

Comments
 (0)