Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Change Pi
- Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14".
- Input
- On the first line you will be given the string
- Output
- On the only output line you should print the result
- Sample tests
- Input
- xpix
- Output
- x3.14x
- Input
- pipi
- Output
- 3.143.14
- import java.util.Scanner;
- public class ChangePi {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- System.out.println(newString(input));
- }
- static String newString(String input) {
- if (input.length() <= 1) {
- return input;
- }
- if (input.charAt(0) == 'p' && input.charAt(1) == 'i') {
- return "3.14" + newString(input.substring(2, input.length()));
- }
- return input.charAt(0) + newString(input.substring(1, input.length()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment