본문 바로가기

코딩테스트/Coding Bat

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

728x90
728x90

https://codingbat.com/prob/p125268

 

CodingBat Java Warmup-1 endUp

Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.

codingbat.com



= 문제 번역 =

문자열이 주어지면 이제 마지막 3개의 문자가 대문자인 새 문자열을 반환합니다. 

문자열이 3자 미만인 경우 존재하는 모든 것을 대문자로 지정합니다. 

+) str.toUpperCase()는 문자열의 대문자 버전을 반환합니다.

 

= 문제푸는 팁 =

d

 

= 해설 =

< 1 >
public String endUp(String str) {
  if(str.length()<=3){
  return str.toUpperCase();
  }else{
    int back = str.length()-3;
    return str.substring(0,back)+str.substring(back,str.length()).toUpperCase();
  }
}

< 2 >
public String endUp(String str) {
  if (str.length() <= 3) return str.toUpperCase();
  int cut = str.length() - 3;
  String front = str.substring(0, cut);
  String back  = str.substring(cut);  // this takes from cut to the end
  
  return front + back.toUpperCase();
}

728x90