Remove Duplicate Characters from a String

Code

#include <stdio.h>
#include <stdint.h>
#include <string.h>
void remove_duplicates(char *str) {

    bool arr[255]={0};
    char ans[strlen(str)+1];
    char *initial=str;
    int count=0;
    while( *str != '\0')
    {
        if( arr[*str] == 0)
        {
            arr[*str]=1;
            ans[count]=*str;
            count++;
        }
        str++;
    }

    ans[count]='\0';
    str=initial;
    strcpy(str,ans);
	//Your logic here
    }

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