File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ package easy ;
2
+
3
+ /**
4
+ * Have the function ExOh(str) take the str parameter being passed
5
+ * and return the string true if there is an equal number of x's and o's,
6
+ * otherwise return the string false. Only these two letters
7
+ * will be entered in the string, no punctuation or numbers.
8
+ * For example: if str is "xooxxxxooxo" then the output
9
+ * should return false because there are 6 x's and 5 o's.
10
+ */
11
+ public class ExOh {
12
+
13
+ /**
14
+ * EX OH function.
15
+ *
16
+ * @param str input string
17
+ * @return "true" if there is an equal number of x's and o's
18
+ */
19
+ private static String exOh (String str ) {
20
+ int balance = 0 ;
21
+ String [] letters = str .toLowerCase ().split ("" );
22
+ for (String l : letters ) {
23
+ switch (l ) {
24
+ case "x" :
25
+ balance ++;
26
+ break ;
27
+ case "o" :
28
+ balance --;
29
+ break ;
30
+ default :
31
+ break ;
32
+ }
33
+ }
34
+ return balance == 0 || str .length () == 0 ? "true" : "false" ;
35
+ }
36
+
37
+ /**
38
+ * Entry point.
39
+ *
40
+ * @param args command line arguments
41
+ */
42
+ public static void main (String [] args ) {
43
+ var result1 = exOh ("xxxooo" );
44
+ System .out .println (result1 );
45
+ var result2 = exOh ("xxxoo" );
46
+ System .out .println (result2 );
47
+ }
48
+
49
+ }
You can’t perform that action at this time.
0 commit comments