'Sorted string after removing the same characters not working right

Overall the code is working except one part. The code first outputs sorted string in alphabetical order. Then the repeated characters are removed so each letter is displayed just once. My problem here is when I input "datastructures" it displays acdersttu, but instead I should have acderstu, which means with only one t. Where is the problem? My code:

#include <stdio.h>
#include <string.h>

int main ()
{
    char str[100];
    int freq[256] = {0};
    char temp;
    int i, j, k;
    printf("\nEnter the string: ");
    scanf("%s",str);

    int n = strlen(str);
    for (i = 0; i < n; i++) {
        for (j = i+1; j < n; j++) {
            if (str[i] > str[j]) {
                    temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
            }
        }
    }
    printf("The sorted string is: %s", str);

    for(i = 0; i < n; 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("\nThe sorted string after removing same characters is: %s ", str);
    return 0;
}


Solution 1:[1]

An alternative way to remove adjacent duplicate characters uses a single loop instead of three nested loops:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Remove adjacent duplicate characters */
static void remove_duplicates(char *str)
{
    char *src = str;
    char *dst = str;
    char c = *dst = *src++;

    while (c != '\0')
    {
        c = *src++;
        if (c != *dst)
            *++dst = c;
    }
}

/* Comparison function for qsort() - comparing characters */
static int cmp_char(const void *v1, const void *v2)
{
    int c1 = (unsigned char)*(const char *)v1;
    int c2 = (unsigned char)*(const char *)v2;
    return c1 - c2;
}

static void test(char *str)
{
    size_t length = strlen(str);
    printf("Before sort (%zu): [%s]\n", length, str);
    qsort(str, length, sizeof(char), cmp_char);
    printf("After  sort (%zu): [%s]\n", strlen(str), str);
    remove_duplicates(str);
    printf("After removing duplicates (%zu): [%s]\n", strlen(str), str);
}

int main(void)
{
    char str1[] = "datastructures";
    test(str1);

    char str2[] = "aaaaaaaaaaa";
    test(str2);

    char str3[] = "dbca";
    test(str3);

    char str4[] = "z";
    test(str4);

    char str5[] = "";
    test(str5);

    return 0;
}

Output

Before sort (14): [datastructures]
After  sort (14): [aacderrsstttuu]
After removing duplicates (8): [acderstu]
Before sort (11): [aaaaaaaaaaa]
After  sort (11): [aaaaaaaaaaa]
After removing duplicates (1): [a]
Before sort (4): [dbca]
After  sort (4): [abcd]
After removing duplicates (4): [abcd]
Before sort (1): [z]
After  sort (1): [z]
After removing duplicates (1): [z]
Before sort (0): []
After  sort (0): []
After removing duplicates (0): []

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan Leffler