Check my code for replace pi using recursion

link: https://ide.codingblocks.com/s/86372
i am getting the error of String index out of bounds. Earlier to resolve this issue by changing the base case . but this time i do not understand how to fix this. This occurs many time please tell me how handle this case. is possible give me any other approach to solve this problem.

Hi,take input of string as sc.next instead of sc.nextLine().