|
@@ -5,30 +5,74 @@ package rocks.zipcode.quiz5.fundamentals;
|
5
|
5
|
*/
|
6
|
6
|
public class StringUtils {
|
7
|
7
|
public static Character getMiddleCharacter(String string) {
|
8
|
|
- return null;
|
|
8
|
+ return string.charAt(string.length() / 2);
|
9
|
9
|
}
|
10
|
10
|
|
11
|
11
|
public static String capitalizeMiddleCharacter(String str) {
|
12
|
|
- return null;
|
|
12
|
+ int midIndex = str.length() / 2;
|
|
13
|
+ return str.substring(0, midIndex) + str.substring(midIndex, midIndex + 1).toUpperCase() + str.substring(midIndex + 1);
|
13
|
14
|
}
|
14
|
15
|
|
15
|
16
|
public static String lowerCaseMiddleCharacter(String str) {
|
16
|
|
- return null;
|
|
17
|
+ int midIndex = str.length() / 2;
|
|
18
|
+ return str.substring(0, midIndex) + str.substring(midIndex, midIndex + 1).toLowerCase() + str.substring(midIndex + 1);
|
17
|
19
|
}
|
18
|
20
|
|
19
|
21
|
public static Boolean isIsogram(String str) {
|
20
|
|
- return null;
|
|
22
|
+ char[] stringChars = str.toCharArray();
|
|
23
|
+ for (int i = 0; i < str.length(); i++) {
|
|
24
|
+ for (int j = 0; j < str.length(); j++) {
|
|
25
|
+ if (i != j) {
|
|
26
|
+ if (stringChars[i] == stringChars[j]) {
|
|
27
|
+ return false;
|
|
28
|
+ }
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+ return true;
|
21
|
33
|
}
|
22
|
34
|
|
23
|
35
|
public static Boolean hasDuplicateConsecutiveCharacters(String str) {
|
24
|
|
- return null;
|
|
36
|
+ char[] stringChars = str.toCharArray();
|
|
37
|
+ for (int i = 1; i < str.length(); i++) {
|
|
38
|
+ if (stringChars[i] == stringChars[i - 1]) {
|
|
39
|
+ return true;
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+ return false;
|
25
|
43
|
}
|
26
|
44
|
|
27
|
45
|
public static String removeConsecutiveDuplicateCharacters(String str) {
|
28
|
|
- return null;
|
|
46
|
+ String result = "";
|
|
47
|
+ char[] stringChars = str.toCharArray();
|
|
48
|
+ for (int i = 1; i < str.length(); i++) {
|
|
49
|
+ if (stringChars[i] == stringChars[i - 1]) {
|
|
50
|
+ stringChars[i] = ' ';
|
|
51
|
+ stringChars[i - 1] = ' ';
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+ for (char letter : stringChars) {
|
|
55
|
+ if (letter != ' ') {
|
|
56
|
+ result += letter;
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+ return result;
|
29
|
60
|
}
|
30
|
61
|
|
31
|
62
|
public static String invertCasing(String str) {
|
32
|
|
- return null;
|
|
63
|
+ char[] stringChars = str.toCharArray();
|
|
64
|
+ String result = "";
|
|
65
|
+ for (int i = 0; i < str.length(); i++) {
|
|
66
|
+ if (stringChars[i]>96 && stringChars[i]<123) {
|
|
67
|
+ stringChars[i] = (char) (stringChars[i] - 32);
|
|
68
|
+ }else if (stringChars[i]>64 && stringChars[i]<91){
|
|
69
|
+ stringChars[i] = (char) (stringChars[i] + 32);
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ for (char letter : stringChars) {
|
|
73
|
+ result += letter;
|
|
74
|
+ }
|
|
75
|
+ return result;
|
33
|
76
|
}
|
|
77
|
+
|
34
|
78
|
}
|