-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqueakyClean.java
55 lines (55 loc) · 2.13 KB
/
SqueakyClean.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class SqueakyClean {
static String clean(String identifier) {
// task 1
identifier = identifier.replace(" ", "_");
// task 3
boolean nextCharFlag = false;
for (int i = 0; i < identifier.length(); i++) {
char currentChar = identifier.charAt(i);
// task 2
if (Character.isISOControl(currentChar)) {
if (i + 1 != identifier.length()) {
identifier = identifier.substring(0, i) + "CTRL" + identifier.substring(i + 1, identifier.length());
} else {
identifier = identifier.substring(0, i) + "CTRL";
}
i+=3;
continue;
}
// task 3
if (currentChar == '-') {
nextCharFlag = true;
}
// task 4
if (!Character.isLetter(currentChar) && currentChar != '_') {
if (i + 1 != identifier.length()) {
identifier = identifier.substring(0, i) + identifier.substring(i+1, identifier.length());
} else {
identifier = identifier.substring(0, i);
}
i--;
continue;
}
// task 5
if ('α' <= currentChar && currentChar <= 'ω') {
if (i + 1 != identifier.length()) {
identifier = identifier.substring(0, i) + identifier.substring(i+1, identifier.length());
} else {
identifier = identifier.substring(0, i);
}
i--;
continue;
}
// task 3
if (nextCharFlag) {
if (i + 1 != identifier.length()) {
identifier = identifier.substring(0, i) + Character.toUpperCase(identifier.charAt(i)) + identifier.substring(i+1, identifier.length());
} else {
identifier = identifier.substring(0, i) + Character.toUpperCase(identifier.charAt(i));
}
nextCharFlag = false;
}
}
return identifier;
}
}