Please Help in the ques

Please Help in the ques

hello @anmolaggarwal.432

the idea is to put a recursive function on the Array and print * if we got any repetitive character.

Algorithm:

  • Obtain the result for substring starting at index 1. Store this in another string , say ‘ros’ .
  • Check whether the first character of current string and first character of ros are same.
  • If so return , add a " * " to the new result and concatenate as s[0] + " * " + ros.
  • Else simply return s[0] + ros.
string duplicateChar(string s)
{
    if (s.size() <= 1)
    {
        return s;
    }

    string ros = duplicateChar(s.substr(1));

    if (s[0] == ros[0])
    {
        return s[0] + string("*") + ros;
    }

    return s[0] + ros;
}

I wrote the code again and instead i was using a pointer i in the function instead of substr why was it showing run -time error and i wwas just adding “" in the if instead of string("”) what is the difference

@anmolaggarwal.432
pls share ur code

no that is not right.

u are never reducing the size of s , so base i.e s.size() < =1 will never become true and hence infinte recursion

and what about string("*") and line 8

that is fine.

I am asking I didn’t add it like string ("*") I just add it normal it was showing error

on cb ide ,even in normal case it should not give error , output might be weird becuase we are trying to add string("*") to char (s[0]).

which ide u r using?

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.