Question:
i have a string like "aabbcc" then how to remove duplicate characters from the string in C?
Vish Akha
2014-10-12 02:01:01 UTC
i have a string like "aabbcc" then how to remove duplicate characters from the string in C?
Five answers:
husoski
2014-10-12 02:11:01 UTC
If you only want to remove consecutive duplicates, that's easy. Set up a normal string-to-string copy, but inside the loop only copy the character if (it's the first character) OR (it's different from the previous source character).



If you want to remove the 2nd 'a' in "abbacdef", that's more complicated. A nested inner loop to search from 0 to i-1 for an occurrence of str[i] is probbly the simplest.
cja
2014-10-15 13:06:06 UTC
Here's something to get you started ... similar functionality as given by a previous responder, but much simpler.



#include

#include



#define MAX_LINE_LEN 1024



size_t removeConsecutiveDuplicates(char *a, int n);



int main(int argc, char *argv[]) {

    char in[MAX_LINE_LEN], *p;



    while (1) {

        printf("\n> ");

        fgets(in, MAX_LINE_LEN, stdin);

        if ((p = strchr(in, '\n')) != NULL) *p = '\0';

        removeConsecutiveDuplicates(in, strlen(in) + 1);

        printf("\"%s\"\n", in);

    }

    return 0;

}



/*

  * Delete consecutive duplicate entries in the given array of char.

  * Return the new size of the array.

  */

size_t removeConsecutiveDuplicates(char *a, int n) {

    int newSize = n, i = 0, j;



    while (i < newSize - 1) {

        j = i + 1;

        while ((j < newSize) && (a[j] == a[i])) ++j;

        if ((j - i) > 1) {

            if (j < newSize) {

                memcpy(&a[i+1], &a[j], (n-j)*sizeof(int));

            }

            newSize -= (j - i - 1);

        }

        ++i;

    }

    return newSize;

}



#if 0



Sample run:



> hello

"helo"



> aaabbcccc

"abc"



> xyz

"xyz"



> z

"z"



> 444

"4"



#endif
?
2014-10-12 03:09:54 UTC
This is my suggestion for you very concrete example; use strcpy to remove duplicate characters!

#include

#include

int main()

{

char hello[] = "aabbcc";

strcpy(hello, hello + 1);

strcpy(hello + 1, hello + 2);

strcpy(hello + 2, hello + 3);

printf("%s\n", hello);

return 0;

}
Bob
2014-10-12 06:24:48 UTC
// Note: this only works for consecutive duplicate characters but it can be modified to work for

// non-consecutive duplicate characters.



#include

#include

#include



void removeDuplicates(char str[])

{

int i, j, k, n, duplicateFound = 0, counter = 0;

char *newStr = "", *tempStr = "";



for(i = 0; i < strlen(str); i++)

{

for(j = i + 1; j < strlen(str); j++)

{

if(str[i] == str[j])

{

duplicateFound = 1;

break;

}

}



if(!duplicateFound)

{

tempStr = (char*)malloc((counter + 1) * sizeof(char));

strcpy(tempStr, newStr);

newStr = (char*)malloc((counter + 1) * sizeof(char));

strcpy(newStr, tempStr);

newStr[counter++] = str[i];

}

else

{

tempStr = (char*)malloc((counter + 1) * sizeof(char));

strcpy(tempStr, newStr);

newStr = (char*)malloc((counter + 1) * sizeof(char));

strcpy(newStr, tempStr);

newStr[counter++] = str[i];

duplicateFound = 0;



k = i + 1;



while(k != strlen(str) && str[k] == str[i])

k++;



i = k - 1;

}

}



tempStr = (char*)malloc((counter + 1) * sizeof(char));

strcpy(tempStr, newStr);

newStr = (char*)malloc((counter + 1) * sizeof(char));

tempStr[counter] = '\0';

strcpy(newStr, tempStr);



printf("%s\n\n", newStr);



free(tempStr);

free(newStr);

}



int main()

{

char str[] = "aaaabbbbbbbbbccccccccdddeefghhhhhhh";



removeDuplicates(str);



return 0;

}
Vish Akha
2014-10-12 08:06:27 UTC
is there any easy solution for this?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...