Here is my try to remove duplicates of a string, and I have two questions:
void removeDuplicates(char *original_string)
{
if(original_string == NULL) {
return;
}
int len = strlen(original_string);
if (len < 2) {
return;
}
int tail = 1;
int i;
for (i = 1; i < len; i++) {
int j;
for (j=0; j < tail; j++) {
if (original_string[i] == original_string[j]) {
break;
}
}
if (j == tail) {
original_string[tail] = original_string[i];
++tail;
}
}
}
First: What am I doing wrong that I don't see? I have found this example in a book and I believe it makes sense. Why are the duplicated characters not being deleted?
Second: When calling the function, if I do it with:
char duplicated[] = "aba";
removeDuplicates(duplicated);
I don't get an error. But if I do it with:
char *duplicated = "aba";
removeDuplicates(duplicated);
I get an Bus error: 10
in run time.