Run time error in given code. what do i need to change

public static String dup(String str)
{
String ans="";

    if(str.substring(0)=="")
    {
        return ans ;
    }

    char c1=str.charAt(0);
    String ros=str.substring(1);

    ans=dup(ros);
    if(c1==ans.charAt(0))
    {
        return c1+"*"+ans;
    }
    else
    {
        return c1+ans;
    }
}

error is becoz of u have ans length==0
and u trying to access its first char

hope this will help u
pls rate my work

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.