File tree Expand file tree Collapse file tree 1 file changed +10
-6
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 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) {
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}
You can’t perform that action at this time.
0 commit comments