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.length() == 1) str = str.substring(0,1); else if (str.length() == 0) str = ""; else if ( !(str.charAt(0) == 'o') && !(str.charAt(1) == 'z') ) str = ""; else if ( str.charAt(0) == 'o' && str.charAt(1) != 'z') str = str.substring(0,1); else if (str.charAt(1) == 'z' && str.charAt(0) != 'o') str = str.substring(1,2); else if ( str.charAt(0) =='o' && str.charAt(1) == 'z') str = str.substring(0,2); return str; }
No comments :
Post a Comment