The following code I found on tutorialgateway is used to remove all duplicate characters from a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, k;
printf("\n Please Enter any String : ");
gets(str);
for(i = 0; i < strlen(str); i++)
{
for(j = i + 1; str[j] != '\0'; j++)
{
if(str[j] == str[i])
{
for(k = j; str[k] != '\0'; k++)
{
str[k] = str[k + 1];
}
}
}
}
printf("\n The Final String after Removing All Duplicates = %s \n", str);
return 0;
}
I thought if I changed the == for != on the if statement it would instead remove all unique or non duplicate characters, but instead it does something else. What am I not understanding about how this code works?