Replace all with pi - Is my approach correct?

here’s my code which passes the test case but is very different from editorial.
is my approach correct?
https://ide.codingblocks.com/s/59728

#include
#include
using namespace std;
string output;
void replace(string s, int i){
if(s[i] == ‘\0’){
cout<<output<<endl;
output = “”;
return;
}

if((s[i] == 'p') && (s[i+1] == 'i')){
  output += "3.14";
  i++;
}
else{
    output += s[i];
}
replace(s,i+1);

}

int main() {
int test;
cin>>test;
while(test–){
string s;
cin>>s;
replace(s,0);
}
}

Hi Lakshay, the code given in editorial is replacing pi with 3.14 inplace. You are appending in a new string. Your code is also correct.

1 Like