[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Show all posible combinations of letters according to length

Posted on 2005-04-13
8
Medium Priority
?
184 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:wcoka
  • 4
  • 3
8 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 13780127
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
 
LVL 17

Accepted Solution

by:
Thibault St john Cholmondeley-ffeatherstonehaugh the 2nd earned 2000 total points
ID: 13781091
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13781374
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17
ID: 13788690
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13789079
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
 
LVL 17
ID: 13789131
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13797441
>> 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
 
LVL 17
ID: 13832384
>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

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.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

831 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