Tuesday, February 22, 2011

JAVA BAT : String 1 : extraEnd

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Extra End Problem : Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
Examples:
extraEnd("Hello") → "lololo"
extraEnd("ab") → "ababab"
extraEnd("Hi") → "HiHiHi"

Solution for this JavaBat extraEnd problem:

public String extraEnd(String str) {

  String lastchars = str.substring(str.length()-2,str.length());
  return lastchars+lastchars+lastchars;

}


4 comments:

  1. Thnx 4 al tha cobes it rly help me wif cumpoter cience. I blame Obama and wan mo help. Congratulatiounes on the blogd.

    ReplyDelete
  2. really helpful. I'm taking a DukeU class through Coursera and this is the perfect compliment to fill in the gaps of my understanding.

    ReplyDelete