Recursive program to replace all occurrences of pi with 3.14 in a given string | C ++ 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 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string replacePi ( string s ) { if ( s . length ( ) == 0 || s . length ( ) == 1 ) return s ; if ( s [ 0 ] == ' p ' && s [ 1 ] == ' i ' ) { string smallOutput = replacePi ( s . substr ( 2 ) ) ; return "3.14" + smallOutput ; } else { return s [ 0 ] + replacePi ( s . substr ( 1 ) ) ; } } int main ( ) { string s = " pipppiiipi " ; string result = replacePi ( s ) ; cout << result << endl ; return 0 ; }