top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Remove duplicate characters from given string using C?

+2 votes
1,054 views

Remove duplicate characters from given string?

Input: "cutcopypaste"
Output: "uoyase"

posted Feb 8, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

Logic is simple take a bucket of 256 elements and mark the specific array element as true if the element is not found and copy in the target string. If element is not found just continue and dont copy in the target. See the following code implementation -

#include <stdio.h>
#include <stdlib.h>
#define NO_OF_CHARS 256
#define bool int

char *removeDups(char *str)
{
  bool bin_hash[NO_OF_CHARS] = {0};
  int ip_ind = 0, res_ind = 0; 
  char temp;    


  while (*(str + ip_ind))
  {
    temp = *(str + ip_ind);
    if (bin_hash[temp] == 0)
    {
        bin_hash[temp] = 1;
        *(str + res_ind) = *(str + ip_ind);
        res_ind++;         
    }
    ip_ind++;
  }      

  *(str+res_ind) = '\0';   

  return str;
}
answer Mar 10, 2016 by Ashish Kumar Khanna
0 votes
#include <stdio.h>
  #include <string.h>

  int main() {
        char str[256];
        int i, j, k, ch, flag;

        /* get the input string from the user */
        printf("Enter your input string:");
        fgets(str, 256, stdin);
        str[strlen(str) - 1] = '\0';

        /* delete the duplicate characters in i/p string */
        while (str[i] != '\0') {

                /* ch is the current processing character */
                ch = str[i];

                /*
                 * checking whether ch is present in the
                 * remaining part of the string.  If yes,
                 * delete it
                 */
                j = i + 1;
                while (str[j] != '\0') {
                        if (str[j] == ch) {
                                k = j;
                                /* remove duplicates */
                                while (str[k] != '\0') {
                                        str[k] = str[k + 1];
                                        k++;
                                }
                        } else {
                                j++;
                        }
                }
                i++;
        }

        /* print the result */
        printf("After removing duplicates: %s\n", str);
        return 0;
  }

Output:

 Enter your input string : aaabbaccaddeefghhiikaalm
  After removing duplicates: abcdefghiklm
answer Feb 9, 2016 by Shivaranjini
0 votes
// C++ prigram to remove duplicates, the order of
// characters is not maintained in this program
#include<bits/stdc++.h>
using namespace std;

/* Function to remove duplicates in a sorted array */
char *removeDupsSorted(char *str)
{
    int res_ind = 1, ip_ind = 1;

    /* In place removal of duplicate characters*/
    while (*(str + ip_ind))
    {
        if (*(str + ip_ind) != *(str + ip_ind - 1))
        {
            *(str + res_ind) = *(str + ip_ind);
            res_ind++;
        }
        ip_ind++;
    }

    /* After above step string is stringiittg.
       Removing extra iittg after string*/
    *(str + res_ind) = '\0';

    return str;
}


/* Function removes duplicate characters from the string
   This function work in-place and fills null characters
   in the extra space left */
char *removeDups(char *str)
{
   int n = strlen(str);

   // Sort the character array
   sort(str, str+n);

   // Remove duplicates from sorted
   return removeDupsSorted(str);
}

/* Driver program to test removeDups */
int main()
{
  char str[] = "eeeefggkkosss";
  cout << removeDups(str);
  return 0;
}
answer Feb 9, 2016 by Rajan Paswan
Similar Questions
0 votes

Given a string, add some characters to the from of it so that it becomes a palindrome.
e.g.
1) If input is "abc" then "bcabc" should be returned.
2) input -> "ab" output -> "bab"
3) input -> "a" output -> "a"

+1 vote

Given a string and dictionary of words, form a word by removing minimum number of characters.
Characters can be removed in-order only.

...