Java > Warmup-1 > endUp (CodingBat Solution)

Problem:

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.

endUp("Hello") → "HeLLO"
endUp("hi there") → "hi thERE"
endUp("hi") → "HI"


Solution:

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

1 comment:

  1. function endUp(str){
    newStr = str.substring(str.length - 3, str.length);
    UpperStr = newStr.toUpperCase();
    originalStr = str.substring(0, str.length - 3);
    return originalStr + UpperStr;
    }

    ReplyDelete