• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Permutating a word

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
tunmire
Asked:
tunmire
  • 3
  • 2
  • 2
1 Solution
 
beckinghCommented:

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bobbit31Commented:
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
 
AJAY CHADHACommented:
Here is the answer..

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

Good Luck...
Ajay Chadha .. :-)
0
 
AJAY CHADHACommented:
This example will do the trick...

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

Ajay Chadha ..:-)
0
 
bobbit31Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now