Solved

Permutating a word

Posted on 2001-06-14
7
303 Views
Last Modified: 2010-05-02
I have a problem that I have not been able to solve, that is I want to take a word and return all of its permutations. So, far I have not been successful in creating such a function.

For example if a word contains 5 characters then it should return an array of 120 combinations of the letters.

Example of permutation the word ape which has only 6 permutations.
ape
aep
pae
pea
eap
epa
0
Comment
Question by:tunmire
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
beckingh earned 300 total points
ID: 6192436
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6192466
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6192470

Modified version of the code I linked to.  Add a commandbutton (Command1) a textbox (Text1) and  listbox (List1) to a form.  Then add this code.

It will add all the permutations to the listbox so you can see them.  It would be easy enough to add them to an array.

Sub Perm(A() As String, m, n As Integer)
    Dim i As Integer
    Dim Temp As String


    If m = 1 Then
        PrintPerm A, n 'Print out the permutations
       
    Else


        For i = 1 To m
            Temp = A(i)
            A(i) = A(m) 'Exchange A(i) and A(m), could have made a Sub To Do this
            A(m) = Temp
            Perm A, m - 1, n 'Recursive Function call
            Temp = A(m)
            A(m) = A(i) 'Exchange A(m) and A(i)
            A(i) = Temp
        Next i
    End If
End Sub


Private Sub Command1_Click()
    Dim strArray() As String
    Dim llLoop As Integer
    Dim llLength As Integer
    llLength = Len(Text1.Text)
    ReDim strArray(llLength)
   
    For llLoop = 1 To llLength
        strArray(llLoop) = Mid(Text1.Text, llLoop, 1)
    Next
   
    Perm strArray(), llLength, llLength
End Sub


Public Sub PrintPerm(A() As String, n As Integer)
    Dim i As Integer
    Dim strVar As String

    List1.Clear
   
    For i = 1 To n
        strVar = strVar & (A(i))
    Next i
   
    List1.AddItem strVar
End Sub
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 18

Expert Comment

by:bobbit31
ID: 6192501
good thing i checked back in before i started doing it from scratch...  let us know if beckingh's solution isn't sufficient (but i don't see why it wouldn't be)
0
 
LVL 8

Expert Comment

by:AJAY CHADHA
ID: 6192820
Here is the answer..

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20094203

Good Luck...
Ajay Chadha .. :-)
0
 
LVL 8

Expert Comment

by:AJAY CHADHA
ID: 6192822
This example will do the trick...

http://www.websamba.com/chadhaajay/files/permutation.zip

Ajay Chadha ..:-)
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6192884
nice one ajay!  where ya been?

now that i look at the question, this definitely looks like a homework...  oh well, his loss (tunmire's).

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question