Java > Warmup-1 > notString (CodingBat Solution)

Problem:

Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings.

notString("candy") → "not candy"
notString("x") → "not x"
notString("not bad") → "not bad"


Solution:

public String notString(String str) {
  if (str.startsWith("not"))
    return str;
  else
    return "not " + str;
}


1 comment :

  1. public String notString(String str) {
    if (str.length()>=3&&str.substring(0,3).equals("not")) return str;
    return "not " + str;
    }

    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