I'm trying to create a recursive function which removes the consecutive duplicate characters from a string. It works fine except the first few characters. For example if my input is MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL or something like this, output is MMuOKLE OL. As you can see except for the first two M's it works fine. How can I make this work for the first part too?
Here is my code:
#include <stdio.h>
char* remove_duplicates (char* str){
if(*(str+1)!='\0'){
if(*str==*(str+1)){
*(str+1)=*(str+2);
remove_duplicates(str+1);
}
remove_duplicates(str+1);
}
return str;
}
int main()
{
char sample[] = "MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL";
printf("OLD: |%s|\n", sample);
printf("NEW: |%s|\n", remove_duplicates(sample));
return 0;
}