ソースを参照

Most of StringUtils

Nathan Hall 5 年 前
コミット
30bf19b7a7
共有1 個のファイルを変更した85 個の追加7 個の削除を含む
  1. 85
    7
      src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java

+ 85
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java ファイルの表示

@@ -1,34 +1,112 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.util.HashMap;
4
+import java.util.Map;
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
+
12
+        return string.charAt(string.length()/2);
9 13
     }
10 14
 
11 15
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
16
+
17
+        String s = str.valueOf(str.charAt(str.length()/2)).toUpperCase();
18
+
19
+        return str.substring(0, str.length()/2) + s + str.substring(str.length()/2 + 1);
13 20
     }
14 21
 
15 22
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
23
+
24
+        String s = str.valueOf(str.charAt(str.length()/2)).toLowerCase();
25
+
26
+        return str.substring(0, str.length()/2) + s + str.substring(str.length()/2 + 1);
17 27
     }
18 28
 
19 29
     public static Boolean isIsogram(String str) {
20
-        return null;
30
+        Map<Character, Integer> map = new HashMap<>();
31
+
32
+        char[] ch = str.toCharArray();
33
+
34
+        for(char c : ch){
35
+            if (!map.containsKey(c)){
36
+                map.put(c, 1);
37
+            } else {
38
+                int count = map.get(c);
39
+                count++;
40
+                map.put(c, count);
41
+            }
42
+        }
43
+
44
+        if (map.size() == 1) {return false;}
45
+
46
+        Integer test = map.get(ch[0]);
47
+
48
+        for (char key : map.keySet()){
49
+            System.out.println(key);
50
+            if (map.get(key) != test){
51
+                return false;
52
+            }
53
+        }
54
+
55
+        return true;
21 56
     }
22 57
 
23 58
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
59
+
60
+        for (int i = 1; i < str.length(); i++) {
61
+            if (str.charAt(i) == str.charAt(i - 1)){
62
+                return true;
63
+            }
64
+
65
+        }
66
+        return false;
25 67
     }
26 68
 
27 69
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
70
+
71
+        char[] chArr = str.toCharArray();
72
+        StringBuilder builder = new StringBuilder();
73
+        char mark = chArr[0];
74
+        builder.append(mark);
75
+
76
+        for (int i = 1; i < chArr.length; i++) {
77
+
78
+            if (chArr[i] == chArr[i - 1]){
79
+
80
+            } else {
81
+                builder.append(chArr[i]);
82
+            }
83
+
84
+
85
+        }
86
+
87
+        return builder.toString();
29 88
     }
30 89
 
31 90
     public static String invertCasing(String str) {
32
-        return null;
91
+
92
+        char[] chArr = str.toCharArray();
93
+        StringBuilder builder = new StringBuilder();
94
+
95
+        for (char c : chArr){
96
+            if (Character.toString(c).matches("[a-z]")) {
97
+                builder.append(Character.toString(c).toUpperCase());
98
+            } else if (Character.toString(c).matches("[A-Z]")){
99
+                builder.append(Character.toString(c).toLowerCase());
100
+            } else {
101
+                builder.append(Character.toString(c));
102
+            }
103
+        }
104
+
105
+
106
+
107
+
108
+        return builder.toString();
33 109
     }
110
+
111
+
34 112
 }