How to delete duplicates in characters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gujinni
    New Member
    • Jul 2011
    • 2

    How to delete duplicates in characters?

    how to delete duplicates in characters?
    output:
    string:bnbhhggl k
    result:nlk
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    string is an array
    Code:
    cout<<string;//prints bnbhhgglk
    string.erase(0,1);
    cout<<string;//prints nbhhgglk
    string.erase(1,5);
    cout<<string;//prints nlk
    hopefully!

    Comment

    • gujinni
      New Member
      • Jul 2011
      • 2

      #3
      without using erase??

      how about
      without
      using erase???

      what codes do I need to use without using erase or predefined functions???

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Rather than codE, what you need is an algorithm. Then you code the algorithm.

        You might:
        1) create an array of char big enough so there is one element for each letter..
        2) read one character of your string.
        3) Look in the array. The letter A has a value 65 so use element 65 for A's.
        4) If the array element is blank, store the A there and output the character.
        5) If the array element contains 65, then the A you have is a duplicate. Do not output the character.

        Repeat all steps until input is processed.

        Now start coding.

        As a general rule: Only if you can write on paper in your own words what it is you want to do, can you code it.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          In my above post you cannot use string as a name as it is a reserve word. The string would have to be declared with something like string s ="bnbhhgglk" . Clearly this is not the approach you want.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            One possible method would be:
            1. create an array (say s) and populate it with lower case alphabet (a to z)
            2. create a second array and initialize it with the string holding the duplicates.
            3. with two nested loops compare s[0] with s1[0] -> s1[end] and count the number of s[0]`s in s1[].
            4. if there is < 1 or > 1 instances of s[0] in s1[] type char(32) into s[0] otherwise do nothing.
            5.zero the count and repeat for s[1] which holds 'b'
            6.repeat until 'z' is reached.
            7.print out the characters that are not spaces in the s[] array.
            what follows provides an example of how the nested loops finds which lower case letters are not duplicated in the s1[]array.
            Code:
            for(int i=0;i<27;i++) 
                  {count=0;
                   for(int j=0;j<len;j++)
                    if(s[j]==s1[i])count++;
                    for(int j=0;j<len;j++) 
                    if(count<1||count>1)s1[i]= char(32);

            Comment

            Working...