Is there any problems in my code ,or to be improved ?
#include <stdio.h>
#include <stdlib.h>
char * filterdupchar(char* s,int n){
char *tmp2 = s ;
char *tmp =(char*)malloc(n*sizeof(char));
for(int j=0;j<n/2;j++){
int i=0,p=0;
while(i<n){
*(tmp+p) = s[i];
if(s[i]==s[i+1])
i = i+2;
else
i++;
p++;
}
s = tmp;
}
s = tmp2;
return tmp;
}
int main(){
char * str= "bbaaan";
printf("%s",str);
printf("\n");
char * strnew;
strnew = filterdupchar(str,6);
printf("%s",strnew);
return 1;
}
The result should be "ban", but it is appended to something disordered character. In my func , is it necessary to give tmp2 to s, and do i need to free sth.?