Search

Wednesday 7 June 2017

front3

Problem:

Given a string, we'll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.

Test Cases:
front3("Java") → "JavJavJav"
front3("Chocolate") → "ChoChoCho"
front3("abc") → "abcabcabc"

Solution:

public String front3(String str) {
  String re="";
  if(str.length()>=3)
  {
    String k=str.substring(0,3);
    re=k+k+k;
  }
  else if(str.length()<3)
  {
    re=str+str+str;
  }
  return re;
}



No comments:

Post a Comment