Word Scramble

This is a discussion on Word Scramble within the C++ Programming forums, part of the General Programming Boards category; Does anyone know an algorithm to find all possible combinations of letters of a word...

  1. #1
    Unregistered
    Guest

    Word Scramble

    Does anyone know an algorithm to find all possible combinations of letters of a word

  2. #2
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    If you find out the word lenght and put the word in an ARRAY, its probably gonna be very easy to do that
    what does signature stand for?

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Do you mean if you had "ABOFGHIEQ", find all the possible words?

    - or -

    Do you mean if you had "BMJ lives in Chicago", find all the different possible letter combinations?


  4. #4
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Cant you read mister?? Cant you read??
    Does anyone know an algorithm to find all possible combinations of letters of a word
    what does signature stand for?

  5. #5
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Hey, I'm just making sure... people on this board have a habit of poorly phrasing questions; sheesh

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    I've also noticed quite a few people who are very snotty to others. It's not necessary.

    Brendan
    Draco dormiens nunquam titillandus

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does anyone know an algorithm to find all possible combinations of letters of a word
    Yes, but the real question is do you want to find the number of combinations or actually print each combination? If all you are doing is finding N and printing that then the solution is simply finding the factorial of the number of letters in the word:
    Code:
    long perm ( int len )
    {
      int N = 1;
      for ( ; len > 1; len-- )
        N *= len;
      return N;
    }
    Actually printing each combination is more difficult because you have to take a string and rotate the characters accordingly for each combination.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    I actually need to print all the combinations on the screen

  9. #9
    Unregistered
    Guest

    Reiterate

    I think I might need to clarify my dilemma. I need to print into a file using fstream a list of all the possible combinations of a word. The word can be anylength and combinations don't need to be words.

    For example:

    if the user inputs "that" the following list would be produced, but unlike this list it shouldn't have repititions.

    that
    thta
    taht
    tath
    ttha
    ttah
    htat
    htta
    hatt
    hatt
    htat
    htta
    atth
    atht
    ahtt
    ahtt
    atth
    atht
    ttah
    ttha
    that
    thta
    taht
    tath

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think I might need to clarify my dilemma.
    There's no need for clarification. Here is what I meant by rotate the letters in the word:
    Code:
    #include <iostream>
    #include <string>
    
    // No error checking is done, beware.
    
    void rotate ( std::string &s, int len )
    {
      int i, pos = s.length() - len;
      char temp = s[pos];
      for ( i = pos + 1; i < s.length(); i++ )
        s[i - 1] = s[i];
      s[i - 1] = temp;
    }
    
    void perm ( std::string &s, int len )
    {
      if ( len != 1 ) {
        for ( int i = 0; i < len; i++ ) {
          perm ( s, len - 1 );
          if ( len == 2 )
            std::cout<< s <<std::endl;
          rotate ( s, len );
        }
      }
    }
    
    int main()
    {
      std::string word;
      std::cout<<"Enter a word: "<<std::flush;
      std::getline ( std::cin, word );
      perm ( word, word.length() );
      return 0;
    }
    Feel free to use that as a template, but be sure you understand what it is doing, that is have the fun. Now, since I'm sure if I do not tell you this you will come back saying that it doesn't do what you wanted...Of course it doesn't, this prints all combinations. If you want to remove duplicates then you need to save each combination in some kind of list and check each new combination to see if it is already there. This is a project for you, I refuse to write all of your homework.

    Good luck!

    -Prelude
    My best code is written with the delete key.

  11. #11
    Unregistered
    Guest
    my friend forgot to note that the code may not contain arrays
    thanks,
    sm

  12. #12
    Unregistered
    Guest
    y the way our whole class is stumped since all but one person did much programming durring the summer
    (summer jobs)
    so pretty much all of our class is having trouble creating loops
    i thank you for any info you can give that might be of any use
    - sm

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the code may not contain arrays
    My first response to this was "Bwahahaha". This is the second most ridiculous restriction I have heard, the first being to write non-trivial programs in Scheme without using assignments. Yes, it is as hard as it sounds. Could you be specific as to what your exact requirements are so that I can be in a better position to answer accurately?

    -Prelude
    My best code is written with the delete key.

  14. #14
    Unregistered
    Guest
    since we have not formally learned arrays we are not allowed to use them in our programs with few exceptions

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you can't use arrays, use pointer notation. Wherever you have:

    array[x]

    Replace it with:

    *(array+x)

    See how much fun that is?

    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 08:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21