Java > Warmup-1 > startOz (CodingBat Solution)

Problem:

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"


Solution:

public String startOz(String str) {
  if (str.startsWith("oz"))
    return "oz";
  else if (str.startsWith("o"))
    return "o";
  else if (str.startsWith("z", 1))
    return "z";
  else
    return "";
}


1 comment :

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact