All submissions

Remove Duplicate Characters from a String

Code

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

void shift_string(int start,int length,char* str){
    for(;start<length;start++){
        str[start]=str[start+1];
    }
    str[length-1] = '\0';

}

void remove_duplicates(char *str) {
	int str_len = 0;
    while(str[str_len++] != '\0');
    for(int i=0;i<str_len;i++){
        for(int j=i+1;j<str_len;j++){
            if(str[i]==str[j]){
                str_len--;
                shift_string(j,str_len,str);
            }
        }
    }
    
}

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

 

 

 

Loading...

Input

programming

Expected Output

progamin