Recursion duplicate

not all char are getting seprate by *
#include<bits/stdc++.h>
using namespace std;
void replaceduplicates(string arr,int i,int n){
if(n==0){
cout<<arr;
return;
}
if(arr[i]==’\0’)
{
cout<<arr;
return;
}
else if(arr[i]==arr[i+1])
{
// hell o o p \

	arr[n+1]='\0';
  for(int k=n-1;k>i;k--)
  {
  	arr[k+1]=arr[k];
  }
  arr[i+1]='*';
  replaceduplicates(arr,i+2,n+1);

}
else
{
replaceduplicates(arr,i+1,n);

}   

}
int main() {
char arr[10000];
cin>>arr;
replaceduplicates(arr,0,strlen(arr));
return 0;
}

please send the link of code

paste you code at

click on file->save->link will be generated
send the link of code generated
it will look like https://ide.codingblocks.com/s/258793

  1. use character array instead of string in function line no 3
  2. increase n also (line 21)
  3. at line no 16 correct statement
    for(int k=n;k>=i;k–)
    you forgot k>=i (equal to)

Modified Code

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:
if you have more doubts regarding this feel free to ask

1 Like

why k==i? we have to shift all char btw n and i na not i and then after i place * so we should not include i na?

okay
yes this will also work
for(int k=n;k>i;k–)

i hope your doubt is resolved
so please mark it as resolved

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.

great explanation, thanks