Java > String-1 > makeOutWord (CodingBat Solution)

Problem:

Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the out string, e.g. "<>". Note: use str.substring(i, j) to extract the String starting at index i and going up to but not including index j.

makeOutWord("<<>>", "Yay") → "<>"
makeOutWord("<<>>", "WooHoo") → "<>"
makeOutWord("[[]]", "word") → "[[word]]"

Solution:

public String makeOutWord(String out, String word) {
  return out.substring(0,2) + word + out.substring(2,4);
}

1 comment:

  1. public String makeOutWord(String out, String word) {
    return "" + out.charAt(0) + out.charAt(1)
    + word + out.charAt(2) + out.charAt(3);
    }

    ReplyDelete