|
@@ -1,34 +1,68 @@
|
1
|
1
|
package rocks.zipcode.quiz5.fundamentals;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+
|
3
|
6
|
/**
|
4
|
7
|
* @author leon on 21/12/2018.
|
5
|
8
|
*/
|
6
|
9
|
public class StringUtils {
|
7
|
10
|
public static Character getMiddleCharacter(String string) {
|
8
|
|
- return null;
|
|
11
|
+ return string.charAt(string.length()/2);
|
9
|
12
|
}
|
10
|
13
|
|
11
|
14
|
public static String capitalizeMiddleCharacter(String str) {
|
12
|
|
- return null;
|
|
15
|
+ String capMiddle = "";
|
|
16
|
+ capMiddle = (str.substring(0, str.length()/2) + getMiddleCharacter(str).toString().toUpperCase() + str.substring(str.length()/2+1));
|
|
17
|
+ return capMiddle;
|
13
|
18
|
}
|
14
|
19
|
|
15
|
20
|
public static String lowerCaseMiddleCharacter(String str) {
|
16
|
|
- return null;
|
|
21
|
+ String lowMiddle = "";
|
|
22
|
+ lowMiddle = (str.substring(0, str.length()/2) + getMiddleCharacter(str).toString().toLowerCase() + str.substring(str.length()/2+1));
|
|
23
|
+ return lowMiddle;
|
17
|
24
|
}
|
18
|
25
|
|
19
|
26
|
public static Boolean isIsogram(String str) {
|
20
|
|
- return null;
|
|
27
|
+ char arr[] = str.toCharArray();
|
|
28
|
+
|
|
29
|
+ Arrays.sort(arr);
|
|
30
|
+ for(int i = 0; i<arr.length-1; i++){
|
|
31
|
+ if (arr[i] == arr[i+1]) {
|
|
32
|
+ return false;
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+ return true;
|
21
|
36
|
}
|
22
|
37
|
|
23
|
38
|
public static Boolean hasDuplicateConsecutiveCharacters(String str) {
|
24
|
|
- return null;
|
|
39
|
+ char arr[] = str.toCharArray();
|
|
40
|
+
|
|
41
|
+ for(int i = 0; i<arr.length-1; i++){
|
|
42
|
+ if (arr[i] == arr[i+1]) {
|
|
43
|
+ return true;
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+ return false;
|
25
|
47
|
}
|
26
|
48
|
|
27
|
49
|
public static String removeConsecutiveDuplicateCharacters(String str) {
|
28
|
|
- return null;
|
|
50
|
+ if(!hasDuplicateConsecutiveCharacters(str)){
|
|
51
|
+ return str;
|
|
52
|
+ }
|
|
53
|
+ return str;
|
29
|
54
|
}
|
30
|
55
|
|
31
|
56
|
public static String invertCasing(String str) {
|
32
|
|
- return null;
|
|
57
|
+ char[] chars = str.toCharArray();
|
|
58
|
+ for(int i = 0; i < chars.length; i++){
|
|
59
|
+ char c = chars[i];
|
|
60
|
+ if(Character.isUpperCase(c)){
|
|
61
|
+ chars[i] = Character.toLowerCase(c);
|
|
62
|
+ } else if( Character.isLowerCase(c)){
|
|
63
|
+ chars[i] = Character.toUpperCase(c);
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ return new String(chars);
|
33
|
67
|
}
|
34
|
68
|
}
|