Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathUpper.java
More file actions
Latest commit
43 lines (38 loc) · 1.15 KB
/
Copy pathUpper.java
File metadata and controls
43 lines (38 loc) · 1.15 KB
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
40
41
42
43
packagecom.thealgorithms.strings;
publicfinalclassUpper {
privateUpper() {
}
/**
* Driver Code
*/
publicstaticvoidmain(String[] args) {
String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"};
for (Strings : strings) {
asserttoUpperCase(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.
*/
publicstaticStringtoUpperCase(Strings) {
if (s == null) {
thrownewIllegalArgumentException("Input string cannot be null");
}
if (s.isEmpty()) {
returns;
}
StringBuilderresult = newStringBuilder(s.length());
for (inti = 0; i < s.length(); ++i) {
charcurrentChar = s.charAt(i);
if (Character.isLowerCase(currentChar)) {
result.append(Character.toUpperCase(currentChar));
} else {
result.append(currentChar);
}
}
returnresult.toString();
}
}