Replace pi (recursive)
PROBLEM:-Given a string, compute recursively a new string where all appearances of “pi” have been replaced by “3.14”.
xpix
Sample Output :
x3.14x
pipi
Sample Output :
3.143.14
pip
Sample Output :
3.14p
SOLUTION:-
#include <iostream>
using namespace std;
#include<string.h>
void replacePi(char input[]) {
if(input[0]=='\0')
return;
replacePi(input+1);
if(input[0]=='p'&&input[1]=='i')
{
int l=strlen(input);
int i=l;
while(i>=2)
{
input[i+2]=input[i];
i--;
}
input[0]='3';
input[1]='.';
input[2]='1';
input[3]='4';
replacePi(input+4);
}
}
int main() {
char input[10000];
cin.getline(input, 10000);
replacePi(input);
cout << input << endl;
}