Java Exercise
16. 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)
import java.util.Scanner; public class FizzBuzz { public static String fizzBuzz(String str) { if (str.length() > 0) { if (str.charAt(0) == 'f' && str.charAt(str.length() - 1) == 'b') { return "FizzBuzz"; } else if (str.charAt(0) == 'f') { return "Fizz"; } else if (str.charAt(str.length() - 1) == 'b') { return "Buzz"; } } return str; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a string: "); String s = in.nextLine(); System.out.println(fizzBuzz(s)); } }
Get Answers For Free
Most questions answered within 1 hours.