-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapitalizer.java
More file actions
35 lines (33 loc) · 1.12 KB
/
Capitalizer.java
File metadata and controls
35 lines (33 loc) · 1.12 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
public class Capitalizer {
public String changeOnCapitalLetters(String sentence)
{
StringBuilder builder = new StringBuilder();
//to get length of sequence of symbols representing this "sentence"
int len = sentence.length();
if(len == 0)
return "";
//in the current position we locating in word
boolean inWord = false;
int codePoint;
for(int offset = 0;offset < len;)
{
codePoint = sentence.codePointAt(offset);
if(Character.isSpaceChar(codePoint))
{
builder.appendCodePoint(codePoint);
inWord = false;
offset+=Character.charCount(codePoint);
continue;
}
if(inWord)
builder.appendCodePoint(Character.toLowerCase(codePoint));
else
{
builder.appendCodePoint(Character.toTitleCase(codePoint));
inWord = true;
}
offset+=Character.charCount(codePoint);
}
return builder.toString();
}
}