Show all posible combinations of letters according to length

I want that my program start sending to a listbox for example all posible combinations of letters accoring to an specific lengh

for example if I set 4 leters, it should start adding a to listbox
aaaa
aaab
aaac
aaad
....
until
zzzz

How can I do this?
wcokaAsked:
Who is Participating?
 
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
needs a command button, textbox(for the length) and a list box


Option Explicit

Private Sub Command1_Click()
    combs Val(Text1.Text), "c"'change the c for a z to do all combs to z
End Sub

Private Sub combs(length As Integer, maxchar As String)
Dim str As String
str = String$(length, "a")
   ' Debug.Print str
    List1.AddItem str
Do Until str = String$(length, maxchar)
    str = incrc(str, maxchar)
    'Debug.Print str
    List1.AddItem str
Loop

End Sub


Private Function incrc(str As String, maxchar As String) As String
If Len(str) = 0 Then Exit Function 'done
If Right$(str, 1) = maxchar Then
    str = incrc(Left$(str, Len(str) - 1), maxchar)
    str = str & "a"
Else
    str = Left$(str, Len(str) - 1) & Chr(Asc(Right$(str, 1)) + 1)
End If
    incrc = str
End Function
0
 
David LeeCommented:
Hi wcoka,

Well, this isn't the most elegant way, but it works.

Private Sub Command1_Click()
    Dim strBuffer As String
    For w = 1 To 26
        For x = 1 To 26
            For y = 1 To 26
                For z = 1 To 26
                    strBuffer = Chr(96 + w) & Chr(96 + x) & Chr(96 + y) & Chr(96 + z)
                    List1.AddItem strBuffer
                    DoEvents
                Next
            Next
        Next
    Next
End Sub

Cheers!
0
 
fds_fatboyCommented:
How about something like this:

Option Explicit

Private Sub Form_Load()
    List1.Clear
    LetterCombinations 4, List1
End Sub
Private Sub LetterCombinations(StringLength As String, lbx As ListBox)
    Dim strCombination As String
    Dim strEnd As String

    strEnd = String$(StringLength, "z")

    strCombination = String$(StringLength, "a")

    lbx.AddItem strCombination
    Do
        IncrementString strCombination
        lbx.AddItem strCombination
    Loop Until strCombination = strEnd
End Sub

Private Sub IncrementString(InString As String, Optional ByVal Character As Long)
    Dim strChar As String

    If Character = 0 Then Character = Len(InString)
   
    strChar = Mid$(InString, Character, 1)

    If strChar = "z" Then
        Mid$(InString, Character, 1) = "a"
        IncrementString InString, Character - 1
    Else
        Mid$(InString, Character, 1) = Chr$(Asc(strChar) + 1)
    End If
End Sub

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
Thanks wcoka.




fds_fatboy,
Interesting how we both came up with something so similar.
Must be the best way of doing it, or did we both learn at the same place?
0
 
fds_fatboyCommented:
RobinD - I apologise, but when I started typing into the box, your answer hadn't arrived (and I hadn't refreshed).  The two methods are very similar - I believe it is a good way. The main difference is the method of it iteration in the string incrementation.

I did have another ide first of all which I decided against which was simply to iterate a long integer and convert it to base 26.
0
 
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
no need to apologise, I should perhaps for getting there first. (sorry)
I just found it amazing that we came up with such similar methods, even very close names for the procedures and variables.

If you had replied four hours after you did I might have accused you of copying, but it's impossible for that to have happened here :7)

incrc is short for incr with carry, a hang-over from an earlier attempt (didn't change the name), which had the c added to distinguish it from an even earlier failure called incr.

Also interesting that we both had to add the extra print outside the loop - that annoyed me for a while and I had tried 'loop until' rather than 'do until' but found it made no difference - maybe it should inc one more time, but then you run out of alphabet...

0
 
fds_fatboyCommented:
>> Also interesting that we both had to add the extra print outside the loop - that annoyed me for a while and I had tried 'loop until' rather than 'do until' but found it made no difference - maybe it should inc one more time, but then you run out of alphabet...

I grew up with COBOL read ahead data processing. I find it almost natural to put the first process outside the loop. Thinking about it

   strEnd = String$(StringLength + 1, "a")

removing the first additem and moving the increment to the end of the loop would be the way to do it.
0
 
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
>I find it almost natural to put the first process outside the loop

Thank you for that comment. I've always thought it was a failing of mine to not capture the algorithm properly. That single sentence could potentially save me hours of coding time not looking for a solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.