Java > Logic-1 > fizzString (CodingBat Solution)

Problem:

Given a string str, if the string starts with "f" return "Fizz". If the string ends with "b" return "Buzz". If both the "f" and "b" conditions are true, return "FizzBuzz". In all other cases, return the string unchanged. (See also: FizzBuzz Code)

fizzString("fig") → "Fizz"
fizzString("dib") → "Buzz"
fizzString("fib") → "FizzBuzz"


Solution:

public String fizzString(String str) {
    boolean fizz = str.charAt(0) == 'f';
    boolean buzz = str.charAt(str.length() - 1) == 'b';
 
    if (fizz && buzz) return "FizzBuzz";
    if (fizz) return "Fizz";
    if (buzz) return "Buzz";
    return str;
}


8 comments :

  1. public String fizzString(String str) {
    if(str.startsWith("f") && str.endsWith("b"))
    return "FizzBuzz";
    if(str.startsWith("f"))
    return "Fizz";
    if(str.endsWith("b"))
    return "Buzz";
    return str;
    }

    ReplyDelete
  2. public String fizzString(String str) {
    String front = str.substring(0, 1);
    String back = str.substring(str.length()-1, str.length());
    if (front.equals("f") && back.equals("b")) return "FizzBuzz";
    if (front.equals("f")) return "Fizz";
    if (back.equals("b")) return "Buzz";
    return str;
    }

    ReplyDelete
  3. public String fizzString(String str) {

    if(str.startsWith("f")){
    if(str.endsWith("b")){
    return "FizzBuzz";
    }
    return "Fizz";
    }
    if(str.endsWith("b")){
    return "Buzz";
    }
    return str;
    }

    ReplyDelete
  4. public String fizzString(String str) {

    if(str.startsWith("f") && str.endsWith("b"))
    return "FizzBuzz";

    else if(str.startsWith("f"))
    return "Fizz";

    else if(str.endsWith("b"))
    return "Buzz";

    else
    return str;


    }

    ReplyDelete
  5. if((str.startsWith("f"))&&(str.endsWith("b"))) return "FizzBuzz";
    else if(str.startsWith("f")) return "Fizz";
    else if(str.endsWith("b")) return "Buzz";
    else return str;

    ReplyDelete
  6. public String fizzString(String str) {

    if(str.startsWith("f") && str.endsWith("b"))
    return ("FizzBuzz");
    if(str.endsWith("b"))
    return ("Buzz");
    if(str.startsWith("f"))
    return ("Fizz");
    else
    return str;

    }

    ReplyDelete
  7. public String fizzString(String str) {
    if(str.startsWith("f")){
    if(str.endsWith("b")){
    return "FizzBuzz";
    }return "Fizz";
    }
    if(str.endsWith("b"))return "Buzz";
    return str;
    }

    ReplyDelete
  8. String fizzBuzz = "";
    if (str.startsWith("f")){
    fizzBuzz += "Fizz";
    }
    if (str.endsWith("b")) {
    fizzBuzz+= "Buzz";
    }
    if (fizzBuzz.length()<4){
    return str;
    }else
    return fizzBuzz;

    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