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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact