-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathUpper.java
39 lines (36 loc) · 1.12 KB
/
Upper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.thealgorithms.strings;
public final class Upper {
private Upper() {
}
/**
* Driver Code
*/
public static void main(String[] args) {
String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"};
for (String s : strings) {
assert toUpperCase(s).equals(s.toUpperCase());
}
}
/**
* Converts all the characters in this {@code String} to upper case
*
* @param s the string to convert
* @return the {@code String}, converted to uppercase.
*/
public static String toUpperCase(String s) {
if (s == null) {
throw new IllegalArgumentException("Input string connot be null");
}
if (s.isEmpty()) {
return s;
}
StringBuilder result = new StringBuilder(s);
for (int i = 0; i < result.length(); ++i) {
char currentChar = result.charAt(i);
if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
result.setCharAt(i, Character.toUpperCase(currentChar));
}
}
return result.toString();
}
}