Code error-not solvable

The code does not give correct answer if the input string is “xpipippixx” . I know where the problem is,its just i don’t know how to remove it.

Hello @varindavg,

The following statement is causing wrong output:
output=output.substr(0,i)+“3.14”+output.substr(i+2,input.length());
Dry run your code, you would understand the problem.

When you add “3.14” in the output string in replace of “pi”, then you are adding two extra characters in the string.
So, there is no significance of i left as it will lack by two positions in every iteration.

Solution:
There are two ways of doing it:
1.
Keep track of those two extra characters also.

Concatenate in a separate empty string.

  1. In if statement:
    output=output+“3.14”;
    return ReplacePi(input,output,i+2);

  2. In else:
    output=output+a[i];
    return ReplacePi(input,output,i+1);

  3. Remove following statement from main and pass the empty string output:
    output=input;

Hope, this would help.
Give a like, if you are satisfied.