how to remove duplicate chars from a string - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to remove duplicate chars from a string

eatsofteatsoft Posts: 8Member
hi there...

i have a problem here..how to remove duplicate chars from a string
example:
input AAA BBB CCC
output A B C

can anybody help me...

Comments

  • VilanyeVilanye Posts: 684Member
    : hi there...
    :
    : i have a problem here..how to remove duplicate chars from a string
    : example:
    : input AAA BBB CCC
    : output A B C
    :
    : can anybody help me...
    :

    the string::at(int index) function lets you get a hold of individual characters, so you need to have an array to hold all characters. Then make a loop and read each character in the string and compare them to what is in your other array.
  • eatsofteatsoft Posts: 8Member
    : : hi there...
    : :
    : : i have a problem here..how to remove duplicate chars from a string
    : : example:
    : : input AAA BBB CCC
    : : output A B C
    : :
    : : can anybody help me...
    : :
    :
    : the string::at(int index) function lets you get a hold of individual characters, so you need to have an array to hold all characters. Then make a loop and read each character in the string and compare them to what is in your other array.
    :

    then how to remove the duplicated chars, can you show me some code..
    thanks..

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : : : hi there...
    : : :
    : : : i have a problem here..how to remove duplicate chars from a string
    : : : example:
    : : : input AAA BBB CCC
    : : : output A B C
    : : :
    : : : can anybody help me...
    : : :
    : :
    : : the string::at(int index) function lets you get a hold of individual characters, so you need to have an array to hold all characters. Then make a loop and read each character in the string and compare them to what is in your other array.
    : :
    :
    : then how to remove the duplicated chars, can you show me some code..
    : thanks..
    :
    :
    [blue]Here is a simplest way, but not the fastest one:
    [code]
    char* deldupchars (char* s) {
    char* dups = strdup (s);

    if (dups) {
    char* psrc = dups;
    char* pdest = s;
    char ch;

    pdest [0] = '';
    while ((ch = *psrc++) != '') {
    if (! strchr (pdest, ch)) {
    *pdest++ = ch;
    }
    }
    pdest [0] = '';
    free (dups);
    }
    return s;
    }
    [/code]
    [/blue]
  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : hi there...
    :
    : i have a problem here..how to remove duplicate chars from a string
    : example:
    : input AAA BBB CCC
    : output A B C
    :
    : can anybody help me...
    :

    [blue]C-style null terminated character array type of string OR C++ STL string container? If the duplicate characters are always going to be adjacent to each other and C++ is an option, then you could just call the STL [italic]unique[/italic] function and then either following it along with a call to the [italic]erase[/italic] member function (if using C++ STL [italic]string[/italic] containers) or instead just setting a new NULL terminating character if using C-style strings.[/blue]
  • stoberstober Posts: 9,765Member ✭✭✭
    : hi there...
    :
    : i have a problem here..how to remove duplicate chars from a string
    : example:
    : input AAA BBB CCC
    : output A B C
    :
    : can anybody help me...
    :

    Another solution. string str2 contains the final result.
    [code]
    #include
    #include
    using namespace std;

    int main()
    {
    string str1 = "ABC ADC BCA";
    string str2;
    int pos;
    for(int i = 0; i < str1.length(); i++)
    {
    if( (pos = str2.find(str1[i])) < 0)
    str2 += str1[i];

    }
    cout << str2 << endl;
    return 0;
    }
    [/code]
  • eatsofteatsoft Posts: 8Member
    : : hi there...
    : :
    : : i have a problem here..how to remove duplicate chars from a string
    : : example:
    : : input AAA BBB CCC
    : : output A B C
    : :
    : : can anybody help me...
    : :
    :
    : Another solution. string str2 contains the final result.
    : [code]
    : #include
    : #include
    : using namespace std;
    :
    : int main()
    : {
    : string str1 = "ABC ADC BCA";
    : string str2;
    : int pos;
    : for(int i = 0; i < str1.length(); i++)
    : {
    : if( (pos = str2.find(str1[i])) < 0)
    : str2 += str1[i];
    :
    : }
    : cout << str2 << endl;
    : return 0;
    : }
    : [/code]
    :


    its really working, but the solution in C.what method can we use.
  • stoberstober Posts: 9,765Member ✭✭✭
    I know you want us to do your homework for you. You should have enough hints to do it yourself. Post your code and ask questions. Hint: strchr() function will find a chacter in a string.
  • oth8manoth8man Posts: 2Member
    //try to run this
    //BY way can I ask you from where you got this question ?
    #include
    #include
    char* remove(char* s)
    {
    char *temp;
    while(*s)
    {
    if(strchr(temp,*s)==NULL)
    strncat(temp,s,1);
    s++;
    }
    return temp;
    }
    int main()
    {
    char str[100];
    puts("str:");
    gets(str);
    puts(remove(str));
    return 0;
    }

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    Almost 4 years is far too long to be bumping a post.
  • oth8manoth8man Posts: 2Member
    hhhh ~~ wow
    I didn't see the date,,,,Thanks 4 telling me
Sign In or Register to comment.