Why does this code is not working ? because after replacing pi with 3.14 the length of string will obviously change for base case

void replace_pi(string &s, int i) {

if (i == s.length() - 1) {
    return;
}

if (s[i] == 'p' and s[i + 1] == 'i') {
    string s = s.substr(0, i) + (string)"3.14" + s.substr(i + 2);


}

replace_pi(s, i + 1);

}

hello

if this if statement will run then clearly at position i+1 ,i+2,i+3,i+4 will have string 3.14 right?

so u should call replace pi on index i+5 right?

if that if statement will not run then simply make a call on index i+1.

sorry mai smjha niii…

that i+5 will not male any difference… i+1 pe . hoga 3 ke aage wala aur wo pi toh hai nhi toh recursion will simply ignore that and code aage chlta jayega

and i tried as you on i+5 also… the error is same… there is no differnce … kindly run this code and then check

dont declare a new string here

if still it fails,the pls share ur complete code using cb ide

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.