« Replace pi recursively in a string
Problem
Given a string, compute recursively a new string where all appearances of "pi" have been replaced by "3.14".
Constraints :
1 <= |S| <= 50 where |S| represents the length of string S.
Sample Input 1 :
xpix
Sample Output 1 :
x3.14x
Sample Input 2 :
pipi
Sample Output 2 :
3.143.14
Sample Input 3 :
pip
Sample Output 3:
3.14p
Constraints:-
1<=|S|<=50
Solution
1#include <iostream>2#include <iomanip>3#include <algorithm>4#include <string>5#include <cstring>6#include <vector>7#include <cmath>8#include <map>9#include <climits>10// climits for INT_MIN11#include <unordered_map>12using namespace std;1314void replacePi(char input[])15{16 if (input[0] == '\0' || input[1] == '\0')17 {18 return;19 }2021 replacePi(input + 1);2223 if (input[0] == 'p' && input[1] == 'i')24 {25 int length = 0;26 while (input[length] != '\0')27 {28 length++;29 }30 for (int i = length; i >= 2; i--)31 {32 input[i + 2] = input[i];33 }34 length = length + 2;3536 input[0] = '3';37 input[1] = '.';38 input[2] = '1';39 input[3] = '4';40 }41}4243int main()44{45 char input[10000];46 cin.getline(input, 10000);47 replacePi(input);48 cout << input << endl;49 return 0;50}