backAround

Given a string, take the last char and return a new string with the last char added at the front and back, so “cat” yields “tcatt”. The original string will be length 1 or more.

backAround(“cat”) ? “tcatt”
backAround(“Hello”) ? “oHelloo”
backAround(“a”) ? “aaa”

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