Link to home
Start Free TrialLog in
Avatar of wcoka
wcoka

asked on

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?
Avatar of David Lee
David Lee
Flag of United States of America image

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!
ASKER CERTIFIED SOLUTION
Avatar of Enabbar Ocap
Enabbar Ocap
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fds_fatboy
fds_fatboy

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

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?
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.
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...

>> 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.
>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.