front3

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.

front3(“Java”) ? “JavJavJav”
front3(“Chocolate”) ? “ChoChoCho”
front3(“abc”) ? “abcabcabc”

public String front3(String str) {
if(str.length() <3) return str+str+str;
else return str.substring(0,3)+str.substring(0,3)+str.substring(0,3);

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s