-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTitleCase.java
More file actions
29 lines (24 loc) · 885 Bytes
/
Copy pathTitleCase.java
File metadata and controls
29 lines (24 loc) · 885 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
public class TitleCase {
public static void main(String... args) {
String sentence = "stupid is as stupid does";
System.out.print(convertToTitleCase(sentence));
}
public static String convertToTitleCase(String sentence) {
if (sentence.isEmpty()) {
return "";
}
char[] letters = sentence.toCharArray();
boolean capitalizeNext = true;
for (int index = 0; index < letters.length; index++) {
if (Character.isWhitespace(letters[index])) {
capitalizeNext = true;
} else if (capitalizeNext) {
letters[index] = Character.toUpperCase(letters[index]);
capitalizeNext = false;
} else {
letters[index] = Character.toLowerCase(letters[index]);
}
}
return new String(letters);
}
}