sunfresh

Change Pi

Jan 20th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. Change Pi
  2. Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14".
  3.  
  4. Input
  5. On the first line you will be given the string
  6.  
  7. Output
  8. On the only output line you should print the result
  9.  
  10. Sample tests
  11. Input
  12. xpix
  13. Output
  14. x3.14x
  15. Input
  16. pipi
  17. Output
  18. 3.143.14
  19.  
  20. import java.util.Scanner;
  21.  
  22. public class ChangePi {
  23. public static void main(String[] args) {
  24. Scanner scanner = new Scanner(System.in);
  25. String input = scanner.nextLine();
  26. System.out.println(newString(input));
  27. }
  28.  
  29. static String newString(String input) {
  30. if (input.length() <= 1) {
  31. return input;
  32. }
  33. if (input.charAt(0) == 'p' && input.charAt(1) == 'i') {
  34. return "3.14" + newString(input.substring(2, input.length()));
  35. }
  36. return input.charAt(0) + newString(input.substring(1, input.length()));
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment