Saturday, October 31, 2015

remove all duplicates from the input string

/* remove all duplicates from the input string */

#include<stdio.h>
void removeDup(char *str) {
    int str_hash[256] ={0};
    int i;
    int count = 0;
    for (i=0; str[i] != '\0'; i++)
    {
    str_hash[str[i]]++;
        if (str_hash[str[i]] == 1)
            str[count++] = str[i];
    }
    str[count] = '\0';
}

int main() {
    char str[]= "pppppeeeeeeeeeeaaaaaaaaaaaccccccceeeeeee";
    removeDup(str);
    printf("the result string is %s\n", str);
    return 0;
}

No comments:

Post a Comment