File tree 1 file changed +10
-6
lines changed
src/main/java/com/thealgorithms/strings
1 file changed +10
-6
lines changed Original file line number Diff line number Diff line change @@ -21,15 +21,19 @@ public static void main(String[] args) {
21
21
* @return the {@code String}, converted to uppercase.
22
22
*/
23
23
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 ()) {
25
28
return s ;
26
29
}
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 ));
31
35
}
32
36
}
33
- return new String ( values );
37
+ return result . toString ( );
34
38
}
35
39
}
You can’t perform that action at this time.
0 commit comments