본문 바로가기

코딩테스트/Coding Bat

[코딩뱃] [자바] Warmup - 1단계 : startOz 문제

728x90
728x90

 

https://codingbat.com/prob/p199720

 

CodingBat Java Warmup-1 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".

codingbat.com



= 문제 번역 =

문자열이 주어지면 처음 2개의 문자에서 첫 번째 문자는 'o'인 경우에는 o를 산출하고, 두 번째 문자는 'z'인 경우에는 z를 산출합니다.

만약 "ozymandias"가 주어진다면 첫 번째 문자와, 두 번째 문자를 같이 산출하여 "oz"를 산출합니다.

 

= 문제푸는 팁 =

 

= 해설 =

< 1 > 
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;
}

728x90