Java > Warmup-1 > backAround (CodingBat Solution)

Problem:

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"


Solution:

public String backAround(String str) {
  char temp = str.charAt(str.length() - 1);
  return temp + str + temp;
}

1 comment:

  1. return str.charAt(str.length()-1)+str+ str.charAt(str.length()-1);

    ReplyDelete