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;
}

it’s giving test cases error what should be the modified in this code

You should point to the next charater after traversing all repeated ones. But you are doing i+1 in recurrence.

How should i modify it

take a temporary variable j. make it equal to i. then with the help of j iterate repeated elements. Then pass j in recursive function.