startOz

Given a string, return a string made of the first 2 chars (if present), however include first char only if it is ‘o’ and include the second only if it is ‘z’, so “ozymandias” yields “oz”.

startOz(“ozymandias”) ? “oz”
startOz(“bzoo”) ? “z”
startOz(“oxx”) ? “o”

public String startOz(String str) {
String result = "";

if (str.length() >= 1 && str.charAt(0)=='o') {
result = result + str.charAt(0);
}

if (str.length() >= 2 && str.charAt(1)=='z') {
result = result + str.charAt(1);
}

return result;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s