CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Need help eliminating multiples letters

    I want to take in a string and eliminate any multiple letters. An example would be if I input "access", I would likes the output to be "aces". I tried turning the string into an array of characters and then checking each against itself, but I couldn't get it to work, and it didn't really seem like the most straight forward approach.

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Need help eliminating multiples letters

    Quote Originally Posted by blinksumgreen View Post
    I want to take in a string and eliminate any multiple letters.
    You copy the input string to an output string, char by char. If the next char to copy equals the last char you copied you drop it.

  3. #3
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Need help eliminating multiples letters

    The fastest and probably the easiest way would be to convert the string into a char array and work with char pairs. I hope you get my idea... The alternative to this would be regular expressions but imho it's more difficult approach for this case.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  4. #4
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Lightbulb Re: Need help eliminating multiples letters

    i have a code, counts the chars..u can edit codes like what u want..
    i hope to be helpful..
    Code:
    import java.util.Scanner;
    
    public class eleminate {
        public static void main(String[] args) {
            Scanner keyboard=new Scanner(System.in);
            System.out.print("input string :");
            String input=keyboard.nextLine().toLowerCase();   
            char ref;
            for(int i=0;i<input.length();i++)
            {
            ref=input.charAt(i);
            int s=1;
            int o=0;
            for(int k=0; k<i;k++){
                if(ref==input.charAt(k))
                    o++;
            }
            
            if(o!=0) continue;
            for(int j=i+1;j<input.length();j++){
                if(ref==input.charAt(j))
                    s++;
            }
            System.out.println(ref+"-->"+s);
        }
    
    }
    }
    Output:
    Code:
    input string :access
    a-->1
    c-->2
    e-->1
    s-->2

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Need help eliminating multiples letters

    Quote Originally Posted by nuzzle View Post
    You copy the input string to an output string, char by char. If the next char to copy equals the last char you copied you drop it.
    Something like this,
    Code:
    String eliminateMultiples(String s) {
       if (s==null) return null;
       final int N = s.length();
       if (N==0) return "";
       StringBuilder sb = new StringBuilder(N);
       char last = s.charAt(0);
       sb.append(last);
       for (int i=1; i<N; i++) {
          char ch = s.charAt(i);
          if (ch != last) {
             last = ch;
             sb.append(ch);
          }
       }
       return sb.toString();
    }

  6. #6
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Need help eliminating multiples letters

    I came up with the following, but I need some help with the logic. I think the problems lie in two main areas:

    1. If you delete an item, the size will change but won't be able to update the loop.
    2. Even if I do a test run with a word that has no repeating letters, such as "hey", where the length will definitely not change, it still prints out an incorrect statement.


    Code:
    public void removeMultiples(String x) {
    
            String keyword = x;
            StringBuffer sb, temp;
    
            sb = new StringBuffer(keyword);
    
            // reverses the keyword so that multiple letters are removed
            // from the end, not the beginning
            temp = sb.reverse();
    
            for (int i = 0; i < temp.length(); i++) {
    
                for (int j = 1; j < temp.length(); j++) {
    
                    char y = temp.charAt(i);
                    char z = temp.charAt(j);
    
                    // if what is at location y and z match, remove what is at z
                    if(y == z){
                        temp.deleteCharAt(j);
                    }
                }
            }
    
            // corrects the orientation of the keyword
            sb = temp.reverse();
    
            System.out.println(sb);
        }

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Need help eliminating multiples letters

    Did you read Nuzzle's example?

    Have you tried stepping through your code by hand with pencil and paper to see exactly what it does?

    Optimism is an occupational hazard of programming: testing is the treatment...
    K. Beck
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Need help eliminating multiples letters

    Quote Originally Posted by dlorde View Post
    Did you read Nuzzle's example?

    Have you tried stepping through your code by hand with pencil and paper to see exactly what it does?

    Optimism is an occupational hazard of programming: testing is the treatment...
    K. Beck
    Yes, Nuzzle's example only works sometimes. I have tried stepping through the code with paper and pencil and it seems like if there are no repeated letters in the word, then it should work fine, but it doesn't. And in the case of letters being removed, it somehow is able to remove letters and the beginning and end, but not in the middle. When I try and test it, it seems like it should be taking letters out of the beginning and middle, and be jumping out of the for-loop before it gets to the end.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help eliminating multiples letters

    Yes, Nuzzle's example only works sometimes.
    Really, what values doesn't it handle correctly?
    Please provide some examples of what text doesn't work, remembering to tell us the input value, the output value and the expected value.

  10. #10
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Need help eliminating multiples letters

    Quote Originally Posted by keang View Post
    Really, what values doesn't it handle correctly?
    Please provide some examples of what text doesn't work, remembering to tell us the input value, the output value and the expected value.
    The tests I ran were:

    Input~~~~~~~Output

    hello ~~~~~~~ helo
    everyday ~~~~ everyday
    pigglywiggly ~~ piglywigly

    The output for hello is correct.

    everyday should come out as: evryda

    pigglywiggly should come out as: piglyw

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: Need help eliminating multiples letters

    Quote Originally Posted by blinksumgreen View Post
    The output for hello is correct.

    everyday should come out as: evryda

    pigglywiggly should come out as: piglyw
    Well, I misunderstood your requirements. I thought repetitions were to be removed. Instead you want a list of all unique chars with the order of appearance preserved.

    This can be achieved with a slight modification of my previous solution. You just replace the "last" variable with a scan of the output StringBuffer. If a char is already present it's not appended. Here's a pure String version following the same principle,

    Code:
    String eliminateMultiples2(String inStr) {
       if (inStr==null) return null;
       String outStr = "";
       for (int i=0; i<inStr.length(); i++) {
          char ch = inStr.charAt(i);
          if (outStr.indexOf(ch)==-1) outStr = outStr + ch; // append input char to output String if not already present
       }
       return outStr;
    }
    Note that upper and lower case letters are treated as different letters.

    Also not that the solution is quadratic in complexity (O(N*N)). If speed is essential there are linear (O(N)) versions.
    Last edited by nuzzle; September 12th, 2009 at 02:36 AM.

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help eliminating multiples letters

    Quote Originally Posted by nuzzle
    Well, I misunderstood your requirements. I thought repetitions were to be removed.
    You're not the only one. In the initial post the problem wasn't well explained and the example was misleading.

    blinksumgreen: This just goes to show the importance of writing a well worded description of the problem and of giving good examples. A few extra minutes put into your original post would have saved you and us, a lot of time and effort.

  13. #13
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Need help eliminating multiples letters

    Thank you for the help, and I'm sorry about the misunderstanding, that was all on me. I had been working on the problem for a while and in aggravated haste, I posted it on here. I assumed that since I knew what I meant, everyone else would magically as well.

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help eliminating multiples letters

    Don't worry we've all done it at some time or other. The important thing is to have learned from it.

  15. #15
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Need help eliminating multiples letters

    I'm in the final stages of this project and I have one more question.

    Recape + New info - keyword has any non unique characters removed.
    "Alphabet (without J, so as to keep it to 25 letters, and therefore a nice square)" has any characters that were in keyword removed from it.
    Both of these are completed within other methods and it can be assumed that keyword will not have non unique characters, and that alphabet has already had the characters that are in keyword removed.


    Fill in the table with the keyword by rows, and then fill in the rest of the table with the remaining alphabet, by columns. How do I do this? I've tried multidimensional arrays and using mod, but I can't quite figure it out.


    Example:

    Keyword: computer
    alphabet: ABDFGHIKLNQSUVWXYZ

    C O M P U
    T E R N W
    A F I Q X
    B G K S Y
    D H L V Z

    Kind of a hodge podge, so if I didn't explain this well, please let me know.
    Last edited by blinksumgreen; September 14th, 2009 at 12:10 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured