Where am i going wrong?[Recursively replace'pi' with '3.14']

This is what i have to do-
changePi("xpix") → "x3.14x"
changePi("pipi") → "3.143.14"
changePi("pip") → "3.14p"
When i run it, it doesnt print the array, just returns 0;
I just need some guidance to where im going wrong.
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
#include<iostream>
using namespace std;
int changePi(char a[],int i,int j){
char b[100];
if(a[i]=='\0'){
        b[j]=a[i];
for(int k=0;b[k]!='\0';k++){
   cout<<b;

    }
    return 0;
}

else if(a[i]=='p' && a[i+1]=='i'){
b[j]='3';
b[j+1]='.';
b[j+2]='1';
b[j+3]='4';
changePi(a,i+2,j+4);
}
else{
    b[j]=a[i];
    changePi(a,i+1,j+1);
}
}

int main(){
char a[200];
cin.getline(a,100);
changePi(a,0,0);
}
Last edited on
you create another `b' each time you call the function, so when you print at the end, that `b' was uninitialised.
you need to pass it as an extra argument
1
2
3
4
int main(){
   char a[200], b[big_enough];
   //...
   changePi(a, 0, b, 0);
Topic archived. No new replies allowed.