front22

Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so “kitten” yields”kikittenki”. If the string length is less than 2, use whatever chars are there.

front22(“kitten”) ? “kikittenki”
front22(“Ha”) ? “HaHaHa”
front22(“abc”) ? “ababcab”

public String front22(String str) {
int n=str.length();
if(n<2) return(str+str+str);
else return( str.substring(0,2)+str+str.substring(0,2));
}
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