-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCapitalizeWords.java
More file actions
36 lines (22 loc) · 808 Bytes
/
CapitalizeWords.java
File metadata and controls
36 lines (22 loc) · 808 Bytes
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
void main() {
// works for Latin, more complex languages need a different solution
List<String> words = List.of("rock", "forest", "sky", "cloud", "water");
System.out.println(capitalize(words));
System.out.println(capitalize2(words));
}
List<String> capitalize(List<String> words) {
List<String> capitalized = new ArrayList<>();
for (var word : words) {
var sb = new StringBuilder(word);
char c = sb.charAt(0);
char upperCased = Character.toUpperCase(c);
sb.setCharAt(0, upperCased);
capitalized.add(sb.toString());
}
return capitalized;
}
List<String> capitalize2(List<String> words) {
return words.stream()
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.toList();
}