Given a string, compute recursively a new string where all appearances of "pi" have been replaced by "3.14".

PROBLEM
:-
Replace pi (recursive)
Send Feedback

Given a string, compute recursively a new string where all appearances of "pi" have been replaced by "3.14".

Sample Input 1 :
xpix
Sample Output :
x3.14x
Sample Input 2 :
pipi
Sample Output :
3.143.14
Sample Input 3 :
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;
}

Previous
Next Post »

If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon