Remove Duplicate Characters from a String

Code

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

void remove_char(char *str,int index){
    int len=strlen(str);
    if (index>=0 && index<=len){
        memmove(str+index,str+index+1,len-index);
    }
}

void remove_duplicates(char *str) {
    int i=0;
    int len=strlen(str);
    int index=0;
    while(str[i]!='\0'){
        for (int j=i+1;j<len;j++){
            if (str[i]==str[j]){
                index=j;
                remove_char(str,index);
            }
        }
        i++;
    }
    }

int main() {
    char str[101];
    fgets(str, sizeof(str), stdin);

    // Remove newline
    uint8_t i = 0;
    while (str[i]) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        i++;
    }

    remove_duplicates(str);
    printf("%s", str);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

programming

Expected Output

progamin