Recursive program to replace all occurrences of pi with 3.14 in a given string | Java program for above approach ; A simple recursive approach to replace all pi in a given function with "3.14" . Firstly function is declared we don 't  need any helper function one  function is enough ; Base case if s is empty or length of s is 1 return the s ; If the 0 th and 1 st element of s are p and i we have to handle them for rest we have to call recursion it will give the result ; Smalloutput is a variable used to store recursion result ; And we have to add the recursion result with the first part we handled and return the answer ; If not then we have to call recursion from 1 st to all elements then add recursion result to 1 st element and return it ; Driver code ; Function call
class GFG { public static String replacePi ( String s ) { if ( s . length ( ) == 0 || s . length ( ) == 1 ) return s ; if ( s . charAt ( 0 ) == ' p ' && s . charAt ( 1 ) == ' i ' ) { String smallOutput = replacePi ( s . substring ( 2 ) ) ; return "3.14" + smallOutput ; } else { String smallOutput = replacePi ( s . substring ( 1 ) ) ; return s . charAt ( 0 ) + smallOutput ; } } public static void main ( String [ ] args ) { String s = " pipppiiipi " ; String result = replacePi ( s ) ; System . out . println ( result ) ; } }