Skip to content

Commit b07b402

Browse files
committed
EX OH
1 parent 6f0103b commit b07b402

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/easy/ExOh.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

0 commit comments

Comments
 (0)