Skip to content

Commit 246162e

Browse files
authored
Fixed redundant boolean logic (#7151)
1 parent bd7f269 commit 246162e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/main/java/com/thealgorithms/strings/Upper.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@ public static void main(String[] args) {
1515
}
1616

1717
/**
18-
* Converts all the characters in this {@code String} to upper case
18+
* Converts all the characters in this {@code String} to upper case.
1919
*
2020
* @param s the string to convert
2121
* @return the {@code String}, converted to uppercase.
2222
*/
2323
public static String toUpperCase(String s) {
2424
if (s == null) {
25-
throw new IllegalArgumentException("Input string connot be null");
25+
throw new IllegalArgumentException("Input string cannot be null");
2626
}
2727
if (s.isEmpty()) {
2828
return s;
2929
}
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));
30+
31+
StringBuilder result = new StringBuilder(s.length());
32+
33+
for (int i = 0; i < s.length(); ++i) {
34+
char currentChar = s.charAt(i);
35+
if (Character.isLowerCase(currentChar)) {
36+
result.append(Character.toUpperCase(currentChar));
37+
} else {
38+
result.append(currentChar);
3539
}
3640
}
3741
return result.toString();

0 commit comments

Comments
 (0)