Remove Duplicate Characters from a String

Code

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

void remove_duplicates(char *str) {
	//Your logic here
    int start = 0,index1=0;
    int end = 0;
    char *point = str; 
    while(*(str+end) !='\0') end++;
    // printf("start=%d end=%d\n",start, end);
    while(*str!='\0')
    {
        char a = *(str++);
        char *p = str;        
        // printf("a=%c index1=%d start=%d end=%d \n",a,index1,start,end);
        start = ++index1;              
        while (start < end)
        {
            // printf("a=%c *p=%c\n",a,*p);
            if(a==*(p))
            {
                // printf("a=%c *p=%c start=%d\n",a,*p,start);        // programming
                for(int j=(start-index1); j<end; j++)  
                {
                    *(str+j) = *(str+j+1);
                    // printf("*(str+j)=%c *(str+(j+1))=%c\n",*(str+j),*(str+j+1)); 
                }
            }
            start++;
            p++;
        }
    }
}

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