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; }
public String fizzString(String str) {
ReplyDeleteif(str.startsWith("f") && str.endsWith("b"))
return "FizzBuzz";
if(str.startsWith("f"))
return "Fizz";
if(str.endsWith("b"))
return "Buzz";
return str;
}
public String fizzString(String str) {
ReplyDeleteString 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;
}
public String fizzString(String str) {
ReplyDeleteif(str.startsWith("f")){
if(str.endsWith("b")){
return "FizzBuzz";
}
return "Fizz";
}
if(str.endsWith("b")){
return "Buzz";
}
return str;
}
public String fizzString(String str) {
ReplyDeleteif(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;
}
if((str.startsWith("f"))&&(str.endsWith("b"))) return "FizzBuzz";
ReplyDeleteelse if(str.startsWith("f")) return "Fizz";
else if(str.endsWith("b")) return "Buzz";
else return str;
public String fizzString(String str) {
ReplyDeleteif(str.startsWith("f") && str.endsWith("b"))
return ("FizzBuzz");
if(str.endsWith("b"))
return ("Buzz");
if(str.startsWith("f"))
return ("Fizz");
else
return str;
}
public String fizzString(String str) {
ReplyDeleteif(str.startsWith("f")){
if(str.endsWith("b")){
return "FizzBuzz";
}return "Fizz";
}
if(str.endsWith("b"))return "Buzz";
return str;
}
String fizzBuzz = "";
ReplyDeleteif (str.startsWith("f")){
fizzBuzz += "Fizz";
}
if (str.endsWith("b")) {
fizzBuzz+= "Buzz";
}
if (fizzBuzz.length()<4){
return str;
}else
return fizzBuzz;
public String fizzString(String str) {
ReplyDeleteif (str.substring(0,1).equals("f") && str.substring(str.length() -1).equals("b")) {
return "FizzBuzz";
}
if (str.substring(0,1).equals("f")) {
return "Fizz";
}
else if (str.substring(str.length() -1).equals("b")) {
return "Buzz";
}
else {
return str;
}
}