Remove Duplicate Characters from a String

Code

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

void remove_duplicates(char *str) 
{
	//Your logic here
   
    int length=0;
    int count = 0;
    while (str[length] != '\0')
    {
    length ++;
    }
    //printf("%d" , length);
    char res[length] = "";
    for (int i = 0 ; i < length ; i ++)
    {      
        // check if an res :- 
        int flag = 0; 
      //  printf("%d" , length);
        for (int j = 0 ; j <count ; j ++ )
        {
            if (str[i] == res[j])
            {
                // already in the array dont add ; 
                flag = 1; 
                break; 
            }
        }
        if (flag == 0)
        {
           
            res[count] = str[i];
            count ++; 
        }


        
    }
    length=0;
    //str = "";
    while (res[length] != '\0')
    {
    length ++;
    str[length] = res[length];
    }
  // printf("%s\n" , res);
}


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