Monday, September 21, 2015

Return characters in uppercase

---------------------------------------------
class Returnuppercase {
   public static void main(String[] args) {
       String hi = "hello world!";
       System.out.println(hi);
       String upper = hi.toUpperCase();
       System.out.println(upper);
   }
}
---------------------------------------------


Question from Codingbat:
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. answer:


---------------------------------------------
public String endUp(String str) {
    int cut = str.length() - 3;

    if(str.length() > 2) {
        String front = str.substring(0, cut);
        String back  = str.substring(cut, str.length());
        return front + back.toUpperCase();
    }

    return str.toUpperCase();
}
---------------------------------------------