|
@@ -1,34 +1,84 @@
|
1
|
1
|
package rocks.zipcode.quiz5.fundamentals;
|
2
|
2
|
|
|
3
|
+import java.util.*;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* @author leon on 21/12/2018.
|
5
|
7
|
*/
|
6
|
8
|
public class StringUtils {
|
7
|
9
|
public static Character getMiddleCharacter(String string) {
|
8
|
|
- return null;
|
|
10
|
+ return string.charAt(getMiddleIndex(string));
|
|
11
|
+ }
|
|
12
|
+
|
|
13
|
+ public static Integer getMiddleIndex(String string) {
|
|
14
|
+ return string.length() / 2;
|
9
|
15
|
}
|
10
|
16
|
|
11
|
17
|
public static String capitalizeMiddleCharacter(String str) {
|
12
|
|
- return null;
|
|
18
|
+ StringBuilder builder = new StringBuilder(str);
|
|
19
|
+ builder.replace(getMiddleIndex(str), getMiddleIndex(str) + 1, getMiddleCharacter(str).toString().toUpperCase());
|
|
20
|
+
|
|
21
|
+ return builder.toString();
|
13
|
22
|
}
|
14
|
23
|
|
15
|
24
|
public static String lowerCaseMiddleCharacter(String str) {
|
16
|
|
- return null;
|
|
25
|
+ StringBuilder builder = new StringBuilder(str);
|
|
26
|
+ builder.replace(getMiddleIndex(str), getMiddleIndex(str) + 1, getMiddleCharacter(str).toString().toLowerCase());
|
|
27
|
+
|
|
28
|
+ return builder.toString();
|
17
|
29
|
}
|
18
|
30
|
|
19
|
31
|
public static Boolean isIsogram(String str) {
|
20
|
|
- return null;
|
|
32
|
+ List<Character> characters = new ArrayList<>();
|
|
33
|
+
|
|
34
|
+ for (int i = 0; i < str.length(); i++) {
|
|
35
|
+ if (!characters.contains(str.charAt(i))) {
|
|
36
|
+ characters.add(str.charAt(i));
|
|
37
|
+ } else {
|
|
38
|
+ return false;
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ return true;
|
21
|
43
|
}
|
22
|
44
|
|
23
|
45
|
public static Boolean hasDuplicateConsecutiveCharacters(String str) {
|
24
|
|
- return null;
|
|
46
|
+ for (int i = 1; i < str.length(); i++) {
|
|
47
|
+ if (str.charAt(i) == str.charAt(i - 1)) {
|
|
48
|
+ return true;
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ return false;
|
25
|
53
|
}
|
26
|
54
|
|
27
|
55
|
public static String removeConsecutiveDuplicateCharacters(String str) {
|
28
|
|
- return null;
|
|
56
|
+ Set<String> duplicateChars = new HashSet<>();
|
|
57
|
+
|
|
58
|
+ for (int i = 1; i < str.length(); i++) {
|
|
59
|
+ if (str.charAt(i) == str.charAt(i - 1)) {
|
|
60
|
+ duplicateChars.add(str.substring(i, i + 1));
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ for (String character : duplicateChars) {
|
|
65
|
+ str = str.replaceAll(character, "");
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ return str;
|
29
|
69
|
}
|
30
|
70
|
|
31
|
71
|
public static String invertCasing(String str) {
|
32
|
|
- return null;
|
|
72
|
+ StringBuilder builder = new StringBuilder();
|
|
73
|
+
|
|
74
|
+ for (int i = 0; i < str.length(); i++) {
|
|
75
|
+ if (Character.isUpperCase(str.charAt(i))) {
|
|
76
|
+ builder.append(Character.toLowerCase(str.charAt(i)));
|
|
77
|
+ } else {
|
|
78
|
+ builder.append(Character.toUpperCase(str.charAt(i)));
|
|
79
|
+ }
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ return builder.toString();
|
33
|
83
|
}
|
34
|
84
|
}
|