Remove Duplicates

#include<bits/stdc++.h>
using namespace std;

void val(string str,int i,string &out){

if(str[i]=='\0'){
	return ;
}
if(str[i]==str[i+1]){
    for(int k=i;str[k]!='\0';k++){
	    str[k]=str[k+1];
    }
    
}
out+= str[i];
val(str,i+1,out);

}

int main() {
string str,out = " ";
cin>>str;
// cout<<str.length();
val(str,0,out);
cout<<out;
return 0;
}

how should i modify this code its giving test case error

@Somith you can see for eg:- “aaa” your code will fail.
So rather than assigning something to string ‘out’ , you can just modifying the original array and atlast return the index of the modified array till which it contains elements.

Also one thing to note here is string in C++ STL are null terminated so that can also be reason for some wrong output.

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.