-
Notifications
You must be signed in to change notification settings - Fork 10
/
03_replace_pi_recursion.cpp
84 lines (67 loc) · 1.57 KB
/
03_replace_pi_recursion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Topic - Replace Pi Recursion
In a given a string, replace "pi" with "3.14" (inplace i.e without taking any extra space)
Eg: Input : xpighpilmpipi
Output : x3.14gh3.14lm3.143.14
*/
#include <iostream>
using namespace std;
// function to replace pi with 3.14
void replacePi(char c[], int idx)
{
// base case
if( c[idx] == '\0' or c[idx] == '\0')
{
return;
}
// rec case: Look for pi in current position
if(c[idx] == 'p' and c[idx+1] == 'i')
{
// shifting + replacement with 3.14
int j = idx+2; // pi = 2 charcater but 3.14 = 4 characters
// take j to the end of the array (i.e until it points to NULL)
while(c[j] != '\0')
{
j++;
}
// shifting (right to left)
while(j >= idx+2)
{
c[j+2] = c[j];
j--;
}
// replacement with 3.14
c[idx] = '3';
c[idx+1] = '.';
c[idx+2] = '1';
c[idx+3] = '4';
// recursive call on the remaining part
replacePi(c, idx+4);
}
else{
// go to next position
replacePi(c, idx+1);
}
return;
}
// function to drive code
int main()
{
char c[1000];
cout << "Enter characters: ";
cin >> c;
replacePi(c, 0);
cout << "After replacement: ";
cout << c;
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter characters: abcpixyz
After replacement: abc3.14xyz
Case 2:
Enter characters: pistartpimidpiendpi
After replacement: 3.14start3.14mid3.14end3.14
*/