Recursive program to replace all occurrences of pi with 3.14 in a given string | 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 1 st & 2 nd element aren 't "p" & "i", then keep  1st index as it is & call recursion for rest of  the string. ; Driver code ; Function call
function replacePi ( s ) { if ( s . length == 0 s . length == 1 ) return s ; if ( s [ 0 ] == ' ' && s [ 1 ] == ' ' ) { let smallOutput = replacePi ( s . substr ( 2 ) ) ; return " " + smallOutput ; } else { return s [ 0 ] + replacePi ( s . substr ( 1 ) ) ; } } let s = " " ; let result = replacePi ( s ) ; document . write ( result ) ;