Skip to content

Easy challenges #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 12, 2021
Prev Previous commit
Next Next commit
EX OH
  • Loading branch information
ardallie committed Aug 11, 2021
commit b07b402b1495e3aa987705c5f7b2b0b6dae11971
49 changes: 49 additions & 0 deletions src/easy/ExOh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package easy;

/**
* Have the function ExOh(str) take the str parameter being passed
* and return the string true if there is an equal number of x's and o's,
* otherwise return the string false. Only these two letters
* will be entered in the string, no punctuation or numbers.
* For example: if str is "xooxxxxooxo" then the output
* should return false because there are 6 x's and 5 o's.
*/
public class ExOh {

/**
* EX OH function.
*
* @param str input string
* @return "true" if there is an equal number of x's and o's
*/
private static String exOh(String str) {
int balance = 0;
String[] letters = str.toLowerCase().split("");
for (String l : letters) {
switch (l) {
case "x":
balance++;
break;
case "o":
balance--;
break;
default:
break;
}
}
return balance == 0 || str.length() == 0 ? "true" : "false";
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = exOh("xxxooo");
System.out.println(result1);
var result2 = exOh("xxxoo");
System.out.println(result2);
}

}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy