forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplacePi.java
More file actions
38 lines (35 loc) · 943 Bytes
/
ReplacePi.java
File metadata and controls
38 lines (35 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package recursion2.Assignment;
import java.util.Scanner;
/*Given a string, compute recursively a new string where all appearances of "pi" have been replaced by "3.14".
Sample Input 1 :
xpix
Sample Output :
x3.14x
Sample Input 2 :
pipi
Sample Output :
3.143.14
Sample Input 3 :
pip
Sample Output :
3.14p*/
public class ReplacePi {
public static String replacePi(String str) {
String answer = "";
if (str.length() <= 1) {
return str;
}
String smallAnswer = replacePi(str.substring(1));
if (str.charAt(0) == 'p' && smallAnswer.charAt(0) == 'i') {
answer = "3.14" + smallAnswer.substring(1);
} else {
answer = str.charAt(0) + smallAnswer;
}
return answer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
System.out.println(replacePi(str));
}
}