hi all i want to create a program that specify the number of characters for example 4 and it produce all 4 letter combination of English alphabetic .
For example: i say 4 letter combination and it produce for me
abcd
abdc
amnq
bnqr
.....
...
...
... Note: i want all possible combination sorted by order in a textbox AND NO LETTER IS USED MORE THEN ONCE
Hope some one show my how i can produce all possible combination and place it in text box. It would
be nice if i be able to add exception for example dont use certain letters in producing the combination.
Looking forward for replies.Thanks
Last edited by tony007; Apr 28th, 2011 at 09:48 PM.
Re: how to produce combination of 4 letter character?
Note: i want all possible combination sorted by order in a textbox AND NO LETTER IS USED MORE THEN ONE ONCE
Doing 4 letter combinations results in a total of 358,800 possibilities.
And then if you wanted 5 characters it gets MUCH MUCH MUCH MUCH BIGGER. Too many to list in a Listbox Control.
And if you wanted 6 characters or more the total number of possibilities is beyond reason and your computer couldn't handle it and even if it could it would take a VERY VERY VERY LONG time to get your results.
Last edited by jmsrickland; Apr 29th, 2011 at 12:02 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Eh... It's not that bad actually but definitely depends on numbers of letters to permute and there is a formula...
For those who don't like math much take a look at this easy to understand topic (quote below is from that that page).
There are 4 ways to choose the first. 3 ways remain to choose the second, 2 ways to choose the third, and 1 way to choose the last. Therefore the number of permutations of 4 different things is
4· 3· 2· 1 = 24
To summarize the "ABC" will have only 6 unique combinations, "ABCD" - only 24; "ABCDE" - 120; "ABCDEF" - 720; "ABCDEFG" - 5040;
Re: how to produce combination of 4 letter character?
My understanding is that for all possibile n combinations it is n! which is what you are doing.
But that only gives you all combinations for that number of letters. However, he wants all 4 letter combinations out of all 26 letters of the alphabet without any repeats.
Now my understanding of that request is
26! / (26-4)! = 26! / 22! = 4.03291461 × (1026 to the 26th power) divided by 1.12400073 × (1021 to the 20th power) = I don't have the slightest clue
Of course, my understanding may be questionable
Last edited by jmsrickland; Apr 28th, 2011 at 01:50 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Formula you presented might not be understood by those who either hate or don't know math - the ! stands for factorial and I'm sure (with all due respect) many don't even know what the heck it means... Sorry guys.
Anyway, for that reason I had to spell it out:
Re: how to produce combination of 4 letter character?
No. He said 4 letters as an example. I assume he wants to also have 5 letters, 6 letters, etc combinations. But even at 4 letters and if that formular is valid that's still a large number to deal with
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
RB = your assumption was that 4 letters are static... "ABCD" .... but he wants to use the entire alphabet: "ABCD" (all 24 variations) "ABCE" (all 24 variations) "ABCDF" (all 24 variations)..... and so on.... that's how he gets these in there:
amnq
bnqr
Re: how to produce combination of 4 letter character?
Thats what I told him in my post #5
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
He wants what he said he wants.
He wants to take, for example, all 26 letters and arrange them in all 4 letter combinations without repeating any letter in any one combination. That was quite clear in his first post. And in my post #5 I told you exactly that.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
I'm not angry. I'm just stating that the OP made himself clear in his first request and I made it clear (eventhough I didn't explain factorial because I was directing that post to you and I knew you knew what factorial was) in my post #5 which is this:
My understanding is that for all possibile n combinations it is n! which is what you are doing. But that only gives you all combinations for that number of letters. However, he wants all 4 letter combinations out of all 26 letters of the alphabet without any repeats.
Now my understanding of that request is
26! / (26-4)! = 26! / 22! = 4.03291461 × (1026 to the 26th power) divided by 1.12400073 × (1021 to the 20th power) = I don't have the slightest clue
.....and what is not clear about that, may I ask?
Sorry for any misunderstandings on my part.
Last edited by jmsrickland; Apr 28th, 2011 at 03:42 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
26! / 22! = 26 x 25 x 24 x 23 = 358,800
Look at it this way. You can select 26 letters on the first pass, 25 on the second, 24 on the third, and 23 on the fourth without obtaining a repeat while building the 4-character string, assuming that only lower or upper case is acceptable. The starting letter is irrelevant, but you have to exclude on subsequent passes any letter that has already been selected within the string.
Unfortunately, you can't use a sorted list box because you later would have to throw the strings into a text box, and the list is too long. Best bet is to use an array and apply a fast sort routine.
To do 7 letter combinations that would be 3,315,312,000 x 7 (= 23,207,184,000) bytes just to hold the total number of possibitites.
I don't think I even know how to go about writing the code to create these combinations let alone where to store the results.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
This comes rather close. Build a form with a command button and a list box. The list will show the 4-character strings in alphabetical order even without the sorted property set to True.
Code:
Private Sub Command1_Click()
Dim MyStr As String
MyStr = Space$(4)
For I = 97 To 122
Mid$(MyStr, 1, 1) = Chr$(I)
For J = 97 To 122
If J <> I Then
Mid$(MyStr, 2, 1) = Chr$(J)
For K = 97 To 122
If K <> I And K <> J Then
Mid$(MyStr, 3, 1) = Chr$(K)
For L = 97 To 122
If L <> I And L <> J And L <> K Then
Mid$(MyStr, 4, 1) = Chr$(L)
List1.AddItem MyStr ' View Results
End If
Next
End If
Next
End If
Next
Next
End Sub
Building the list box takes the most time. The sorted strings can be shown in an RTF text box but not in a simple text box because the strings require about 1.4 MB.
Re: how to produce combination of 4 letter character?
Many Many thanks for all of you i have to spend time learning form your valuable post.
Code Doc i saved the listbox to text file and it produce for me 31120 different combination ! why it is different then your mathematical calculation ?(26! / 22! = 26 x 25 x 24 x 23 = 358,800)
if i want to allow user to specify the length of combination to produce what part of your program i should modify?
Furthermore for example if i want to use ONLY certain letters in producing the combination how i can achieve that ?
For example :
User types in textbox that he wants 7 letter combination and ONLY use these letters a b d e f k l m n o p q r s t how i can produce 7 letter combination out of specified letters by the user ?
Code:
Private Sub Command1_Click()
Dim MyStr As String
MyStr = Space$(4)
For I = 97 To 122
Mid$(MyStr, 1, 1) = Chr$(I)
For J = 97 To 122
If J <> I Then
Mid$(MyStr, 2, 1) = Chr$(J)
For K = 97 To 122
If K <> I And K <> J Then
Mid$(MyStr, 3, 1) = Chr$(K)
For L = 97 To 122
If L <> I And L <> J And L <> K Then
Mid$(MyStr, 4, 1) = Chr$(L)
List1.AddItem MyStr ' View Results
End If
Next
End If
Next
End If
Next
Next
Label1.Caption = "Total combinations:" & List1.ListCount
End Sub
Private Sub Command2_Click()
Dim intLB As Integer, lngLBItemCount As Long
Dim strPathFile As String, strBuff As String
On Error GoTo Err_Handler:
' CancelError is True.
CommonDialog1.CancelError = True
' Set filters.
CommonDialog1.Filter = "Text Files (*.txt)|*.txt"
' Display the Save dialog box.
CommonDialog1.ShowSave
strPathFile = CommonDialog1.FileName
lngLBItemCount = List1.ListCount - 1
'Iterate past the ListBoxes, building the String Buffer
For intLB = 0 To lngLBItemCount
strBuff = strBuff & List1.List(intLB) & vbCrLf
Next
'Write out the Buffer containing all the data
Open strPathFile For Output As #1
Print #1, strBuff
Close #1
Exit Sub
Err_Handler:
If Err.Number = 32755 Then
'Cancel was pressed, do nothing
Err.Clear
End If
End Sub
Last edited by tony007; Apr 28th, 2011 at 10:23 PM.
Re: how to produce combination of 4 letter character?
Here's why
The ListCount is invalid. So, if you used the ListCount value you will get exactly 31,120. You need to do something like this:
Code:
Private Sub Command1_Click()
Dim MyStr As String
MyStr = Space$(4)
Open MyOutputFile For Output As #1
For I = 97 To 122
Mid$(MyStr, 1, 1) = Chr$(I)
For J = 97 To 122
If J <> I Then
Mid$(MyStr, 2, 1) = Chr$(J)
For K = 97 To 122
If K <> I And K <> J Then
Mid$(MyStr, 3, 1) = Chr$(K)
For L = 97 To 122
If L <> I And L <> J And L <> K Then
Mid$(MyStr, 4, 1) = Chr$(L)
'Commented out--->List1.AddItem MyStr ' View ResultsPrint #1, MyStr
End If
Next
End If
Next
End If
Next
Next
Close #1
Label1.Caption = "Current Item:" & List1.ListCount
End Sub
However, I am not sure that the Listbox held all 358,800 entries. I ran his code and it appeared as though the Listbox did contain all the possibilities (but I cant be sure) but the ListCount starts to go heywire after so many entries (like it goes negative for awhile then it returns to a positive value) so it's final value is 31,120. Even if all 358,800 combinations are in the Listbox you can't get them out because there is no way to count the entries as you will get an overflow exception when you reach entry #32,768 when you try to extract List1.List(32768). Bottom line; don't use a Listbox control.
Last edited by jmsrickland; Apr 28th, 2011 at 11:48 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
You have to use your own file name. MyOutputFile was just a place setter; put your own file name like this:
Open App.Path & "\the_name_of_your_file" For Output As #1
how to fix this erro? does your modification write the result directly to text file without using listbox ?
Yes.
so you suggest i use listview instead ?
I don't know. Here's the problem. I now know that the Listbox does hold all combinations (358,800). What is wrong is that you can't index any item in the listbox above 32,767 otherwise you will get an overflow exception. The ListCount goes from 1 to 32,767 and then it starts to go negative at -32768 and this continues on till it reaches a -1 then it starts at 0 and goes to 32,767 again and then it goes negative again at -32768 back to -1 then starts at 0 again up to 32767 and it does this over and over until the count comes out at 31,120. It not the Listbox itself but rather the ListCount value that gets messed up and also the List1.List(n) if n = 32768. It appears that although you can put all 358,800 items in the box the ListCount and the List(n) values are Integers which have a maximum value of 32,768. So, if you try to put them in a ListView you may still have the same problem with the ListView.ListCount and the ListView.List(n) values. I wrote the results directly to a text file and did not use a Listbox (well I did to see what was going wrong) in the final version. Why don't you try what Code Doc suggested in his post #15 and use an array and then index the array with a Long (not an Integer) variable?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
jmsrickland . Many thanks for your nice explanation. .So you mean after saving the content of listbox to text file i have 358,800 entries in the text file and only ListCount is missed up not showing the actual number of entries ?For me it is good enough to be able to save the combination some where putting it in listbox is not necessary !The reason that i wanted to in listbox is that i was thinking it make sorting the data easier but i think the entries are sorted already in order in text file !
jmsrickland if i want to produce 3 letter combination what part of the code should i modify ?
Last edited by tony007; Apr 29th, 2011 at 01:23 AM.
Re: how to produce combination of 4 letter character?
if i want to produce 3 letter combination what part of the code should i modify ?
Code:
Private Sub Command1_Click()
Dim MyStr As String
MyStr = Space$(3) 'MyStr = Space$(4)
Open App.Path & "\3LetterCombinations.txt" For Output As #1
For i = 97 To 122
Mid$(MyStr, 1, 1) = Chr$(i)
For J = 97 To 122
If J <> i Then
Mid$(MyStr, 2, 1) = Chr$(J)
For K = 97 To 122
If K <> i And K <> J Then
Mid$(MyStr, 3, 1) = Chr$(K)
'For L = 97 To 122' If L <> i And L <> J And L <> K Then' Mid$(MyStr, 4, 1) = Chr$(L)
List1.AddItem MyStr
Print #1, MyStr
DoEvents
' End If'Next
End If
Next
End If
Next
Next
Close #1
End Sub
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Here's the modifications for 5-Letter combinations. Don't even think about putting them in a Listbox or a ListView. There are 7,893,600 combinations
Code:
Private Sub Command1_Click()
Dim MyStr As String
MyStr = Space$(5) 'MyStr = Space$(4)
Open App.Path & "\5LetterCombinations.txt" For Output As #1
For i = 97 To 122
Mid$(MyStr, 1, 1) = Chr$(i)
For J = 97 To 122
If J <> i Then
Mid$(MyStr, 2, 1) = Chr$(J)
For K = 97 To 122
If K <> i And K <> J Then
Mid$(MyStr, 3, 1) = Chr$(K)
For L = 97 To 122
If L <> i And L <> J And L <> K Then
Mid$(MyStr, 4, 1) = Chr$(L)
For M = 97 To 122If M <> i And M <> J And M <> K And M <> L Then
Mid$(MyStr, 5, 1) = Chr$(M)
'List1.AddItem MyStr
Print #1, MyStr
DoEvents
End IfNext
End If
Next
End If
Next
End If
Next
Next
Close #1
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Furthermore for example if i want to use ONLY certain letters in producing the combination how i can achieve that ?
For example :
User types in textbox that he wants 7 letter combination and ONLY use these letters a b d e f k l m n o p q r s t how i can produce 7 letter combination out of specified letters by the user ?
That is getting just a little too complicated. However, if you want 7 letter combinations out of a contineous string like a b c d e f g h i j k l m n o p q r s t then that is reasonable.
Code:
Private Sub Command1_Click()
Dim MyStr As String
Dim Letter1 As Integer
Dim Letter2 As Integer
Dim Letter3 As Integer
Dim Letter4 As Integer
Dim Letter5 As Integer
Dim Letter6 As Integer
Dim Letter7 As Integer
Dim StartLetter As Integer
Dim EndLetter As Integer
Dim Counter As Long
StartLetter = Asc("a")
EndLetter = Asc("t")
MyStr = Space$(7) 'MyStr = Space$(4)
Open App.Path & "\7LetterCombinations.txt" For Output As #1
For Letter1 = StartLetter To EndLetter
Mid$(MyStr, 1, 1) = Chr$(Letter1)
For Letter2 = StartLetter To EndLetter
If Letter2 <> Letter1 Then
Mid$(MyStr, 2, 1) = Chr$(Letter2)
For Letter3 = StartLetter To EndLetter
If Letter3 <> Letter1 And Letter3 <> Letter2 Then
Mid$(MyStr, 3, 1) = Chr$(Letter3)
For Letter4 = StartLetter To EndLetter
If Letter4 <> Letter1 And Letter4 <> Letter2 And Letter4 <> Letter3 Then
Mid$(MyStr, 4, 1) = Chr$(Letter4)
For Letter5 = StartLetter To EndLetter
If Letter5 <> Letter1 And Letter5 <> Letter2 And Letter5 <> Letter3 And Letter5 <> Letter4 Then
Mid$(MyStr, 5, 1) = Chr$(Letter5)
For Letter6 = StartLetter To EndLetter
If Letter6 <> Letter1 And Letter6 <> Letter2 And Letter6 <> Letter3 And Letter6 <> Letter4 And Letter6 <> Letter5 Then
Mid$(MyStr, 6, 1) = Chr$(Letter6)
For Letter7 = StartLetter To EndLetter
If Letter7 <> Letter1 And Letter7 <> Letter2 And Letter7 <> Letter3 And Letter7 <> Letter4 And Letter7 <> Letter5 And Letter7 <> Letter6 Then
Mid$(MyStr, 7, 1) = Chr$(Letter7)
'List1.AddItem MyStr
Print #1, MyStr
DoEvents
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
Next
Close #1
End Sub
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
I like the idea (and speed) of storing the strings on a file, but you might want to save some space (2-bytes per string) by using a random access file.
For the simple 4-byte, 358,800 string case, you save 717,600 bytes. Just a suggestion.
Code:
Private Sub Command1_Click()
Open "MyFile" For Binary As #1
Dim MyStr As String
MyStr = Space$(4)
For I = 97 To 122
Mid$(MyStr, 1, 1) = Chr$(I)
For J = 97 To 122
If J <> I Then
Mid$(MyStr, 2, 1) = Chr$(J)
For K = 97 To 122
If K <> I And K <> J Then
Mid$(MyStr, 3, 1) = Chr$(K)
For L = 97 To 122
If L <> I And L <> J And L <> K Then
Mid$(MyStr, 4, 1) = Chr$(L)
Put #1, , MyStr
End If
Next
End If
Next
End If
Next
Next
Close
End Sub
Once the file is built, OP can use some rather quick access code to extract the ones that he wants because they are all sorted and unique. This thread is starting to bring back some interesting memories.
Last edited by Code Doc; Apr 29th, 2011 at 04:43 PM.
Re: how to produce combination of 4 letter character?
You need to do this:
Put #1, , MyStr & vbCrLf
However, this basically creates the same type of file you had the first time.
The advantage of using an Open For Binary and the Put #1, ,MyStr is that you save 2-bytes per combination from that of using a standard outputfile which uses the vbCrlf
If you don't use the vbCrLf you get one contineous string of data which means you will have to specify the length of the combination in the open statement of a random access file
Here is a simple sample that works. I am opening and closing the file on each call but you should work out a better method that suites your needs
Code:
Private Type Combination
FourLetterCombination As String * 4
End Type
Private Sub SomeSub()
Dim StringData As String
StringData = GetCombination(1)
End Sub
Private Function GetCombination(RecordNumber As Long) As String
Dim C As Combination
Open App.Path & "\your_file_name" For Random Access Read As #1 Len = Len(C)
Get #1, RecordNumber, C ' Read the record specified by RecordNumber
Close #1
GetCombination = C.FourLetterCombination
End Function
This allows you to read in any combination (any record specified by RecordNumber) from the file without having to read the data sequestially. You can, however, read the data sequestially if you want by just starting with RecordNumber = 1 and incrementing it on each call.
Last edited by jmsrickland; Apr 30th, 2011 at 12:53 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Strickland offers a good solution for reading. The idea was to pack the data as tightly as possible without losing any information. This used to be valuable back in the days when mass storage for PCs was a premium.
Using a Binary (or Random Access) file, you know that the 4-byte strings start at the first, fifth, ninth, etc. byte location withing the file for retrieval. You can then set the pointers at any of those locations to retreive a string and optionally add the VbCrLF as need be for screen display. In the case of a list box, that's not even needed.
For example, look how simple it is to retrieve the first 1000 4-byte strings from the file and toss them into a list box:
Code:
Private Sub Command1_Click()
Dim MyData As String
Open "MyFile" For Binary As #1
MyData = Space$(4000)
Get #1, , MyData
For I = 1 To 1000 Step 4
List1.AddItem Mid$(MyData, I, 4)
Next
Close
End Sub
Re: how to produce combination of 4 letter character?
You can do basically the same thing with the random access file mode:
Code:
Private Type Combination
FourLetterCombination As String * 4
End Type
Private Sub SomeSub()
Dim C As Combinations
Open App.Path & "\your_file_name.txt" For Random Access Read As #1 Len = Len(C)
For i = 1 To 1000 'Step 4 <--- commented out
Get #1, i , C
List1.AddItem C.FourLetterCombination
Next
Close #1
End Sub
or like this
Code:
Private Sub SomeSub()
Dim C As Combinations
Open App.Path & "\your_file_name.txt" For Random Access Read As #1 Len = Len(C)
For i = 1 To 4000 Step 4
Get #1, , C
List1.AddItem C.FourLetterCombination
Next
Close #1
End Sub
Except with this method you will have to have the Private Type Combination declaration
Last edited by jmsrickland; Apr 30th, 2011 at 11:07 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
thank u all for this valuable subject
i need it exactly today!
if someone can leave a source of program it's better because as i tried some of the codes put here don't work as tony007 said.
they produce for example combination of words with 4 length but not specified to 4 letter
ex:
abcd
abce
abcf
...
as i saw the problem the formula for combination without repeat is asked.means: n!
and now someone please leave a code or source that for example when we put word "abcd" it gives us these words:
abdc, adbc, bdac, badc, .....
Thanks NOTE: My problem solved. thank u jmsrickland and Code Doc and others.
Last edited by alien1993; May 1st, 2011 at 12:38 AM.
Reason: My Problem Solved!
Re: how to produce combination of 4 letter character?
i tried some of the codes put here don't work as tony007 said.
tony007 never said it didn't work.
If you want all possible combinations of abcd only just use the code Code Doc posted and change the range from 97 To 122 to 97 To 100
Last edited by jmsrickland; May 1st, 2011 at 12:38 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Originally Posted by jmsrickland
i tried some of the codes put here don't work as tony007 said.
tony007 never said it didn't work.
no, no, no i didn't said that tony007 said it doesn't work
but i accept that i wrote my sentence in a bad mood
i had to say "i tried some of the codes put here don't work as tony007 wants."
and now with your help and Code Doc and others my problem solved
Last edited by alien1993; May 1st, 2011 at 12:55 AM.
Re: how to produce combination of 4 letter character?
Tony,
Going back to your post #18 you asked the following question:
User types in textbox that he wants 7 letter combination and ONLY use these letters a b d e f k l m n o p q r s t how i can produce 7 letter combination out of specified letters by the user ?
And in my reply post #27 I stated this:
That is getting just a little too complicated. However, if you want 7 letter combinations out of a contineous string like a b c d e f g h i j k l m n o p q r s t then that is reasonable.
I gave it some more thought and now I consider it not too complicated. If you take the code I posted in #27 which does 7 letter combinations out of all letters from a to t and modify it as below you will get what you asked.
Code:
Private Sub Command1_Click()
Dim MyStr As String
Dim Letter1 As Integer
Dim Letter2 As Integer
Dim Letter3 As Integer
Dim Letter4 As Integer
Dim Letter5 As Integer
Dim Letter6 As Integer
Dim Letter7 As Integer
Dim StartLetter As Integer
Dim EndLetter As Integer
StartLetter = Asc("a")
EndLetter = Asc("t")
MyStr = Space$(7)
Open App.Path & "\7_LetterCombinationsModified.txt" For Binary As #1
For Letter1 = StartLetter To EndLetter
Mid$(MyStr, 1, 1) = Chr$(Letter1)
For Letter2 = StartLetter To EndLetter
If Letter2 <> Letter1 Then
Mid$(MyStr, 2, 1) = Chr$(Letter2)
For Letter3 = StartLetter To EndLetter
If Letter3 <> Letter1 And Letter3 <> Letter2 Then
Mid$(MyStr, 3, 1) = Chr$(Letter3)
For Letter4 = StartLetter To EndLetter
If Letter4 <> Letter1 And Letter4 <> Letter2 And Letter4 <> Letter3 Then
Mid$(MyStr, 4, 1) = Chr$(Letter4)
For Letter5 = StartLetter To EndLetter
If Letter5 <> Letter1 And Letter5 <> Letter2 And Letter5 <> Letter3 And Letter5 <> Letter4 Then
Mid$(MyStr, 5, 1) = Chr$(Letter5)
For Letter6 = StartLetter To EndLetter
If Letter6 <> Letter1 And Letter6 <> Letter2 And Letter6 <> Letter3 And Letter6 <> Letter4 And Letter6 <> Letter5 Then
Mid$(MyStr, 6, 1) = Chr$(Letter6)
For Letter7 = StartLetter To EndLetter
If Letter7 <> Letter1 And Letter7 <> Letter2 And Letter7 <> Letter3 And Letter7 <> Letter4 And Letter7 <> Letter5 And Letter7 <> Letter6 Then
Mid$(MyStr, 7, 1) = Chr$(Letter7)
If InStr(1, MyStr, "c", vbTextCompare) = 0 And _
InStr(1, MyStr, "g", vbTextCompare) = 0 And _
InStr(1, MyStr, "h", vbTextCompare) = 0 And _
InStr(1, MyStr, "i", vbTextCompare) = 0 And _
InStr(1, MyStr, "j", vbTextCompare) = 0 Then
Put #1, , MyStr
DoEvents
End If
End If
Next Letter7
End If
Next Letter6
End If
Next Letter5
End If
Next Letter4
End If
Next Letter3
End If
Next Letter2
Next Letter1
Close #1
End Sub
Writting individual code to produce what you want seems to be reasonable however what you really need is one subroutine that can handle all requests. For this I or someone (like Code Doc, hint, hint) will have to give it some thought. I don't know if I can do that but I will give it a try.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Using string funcmtion is way too slow - it might be faster to convert string to byte array and then swap bytes back and forth.
I'm too lazy to start writing anything but suggest trying that approach.
Re: how to produce combination of 4 letter character?
Here's Code Doc's code modified to give you a single sub to handle user's requests. It's kind of funky and I'm sure anyone can improve on it but it will give you an idea.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to produce combination of 4 letter character?
Originally Posted by RhinoBull
Using string function is way too slow - it might be faster to convert string to byte array and then swap bytes back and forth.
I'm too lazy to start writing anything but suggest trying that approach.
Rhino, I thought about that too, but I figured it might be too complicated for OP to try and understand. Mid$() is rather fast and there might not be much increase in speed when working with byte arrays because my code does avoid concatenation. Eventually, the bytes have to be converted to strings.
Regardless, I hate connecting several And operands coupled with <> probably just as much as you do. I have a feeling there is a way around this using Select ... Case, but I'm getting lazy in my old age also. Sometimes we need AnHn or Logophobic to come to our rescue.