How to generate string by specifies a maximum number of repeated characters?
Dear all expert programmers,
I make string with for .. next loop. I want to specifies a maximum number of repeated characters. But I can't figure out what to do. Please help me to solve this problem.
Code:
private function getGenstring(byval intLimit as byte) as string
dim b as integer, c as integer, d as integer, e as integer, intend as integer
dim strTemp as string
const intend = 4
for b=1 to intend
for c = 1 to intend
for d=1 to intend
for e=1 to intend
strTemp=strTemp & b & c & d & e & ","
next
next
next
next
getGenstr=strTemp
end function
If X=getGenstr(1)
Result will be
1234
If X=getGenstr(2)
Result will be
1122
1123
1124
1133
1134
1144
1223
1224
1233
1234
1244
1334
1344
2233
2234
2244
2334
2344
3344
If X=getGenstr(3)
Result will be
1112
1113
1114
1122
1123
1124
1133
1134
1144
1222
1223
1224
1233
1234
1244
1333
1334
1344
1444
2223
2224
2233
2234
2244
2333
2334
2344
2444
3334
3344
3444
Re: How to generate string by specifies a maximum number of repeated characters?
Your code contains a few flaws. Had you used "Option Explicit" (something I always recommend) you would have noticed you are assigning what is supposed to be your function's return value to an undeclared variable. Also why are you declaring "intend" as a variable only to declare it as a constant afterwards? Also, there is no reason to use the Integer/Byte data types here. It's not going to save memory or make your code faster. Unless you specifically need to work with bytes or 16-bit integers you should long integers in vb6.
Here's you code after I fixed it:
Code:
Option Explicit
Private Const intend As Long = 4
Public Function getGenstring(intLimit As Long) As String
Dim b As Long
Dim c As Long
Dim d As Long
Dim e As Long
Dim strTemp As String
For b = 1 To intend
For c = 1 To intend
For d = 1 To intend
For e = 1 To intend
strTemp = strTemp & b & c & d & e & ","
Next e
Next d
Next c
Next b
getGenstring = strTemp
End Function
Re: How to generate string by specifies a maximum number of repeated characters?
Originally Posted by Peter Swinkels
Your code contains a few flaws. Had you used "Option Explicit" (something I always recommend) you would have noticed you are assigning what is supposed to be your function's return value to an undeclared variable. Also why are you declaring "intend" as a variable only to declare it as a constant afterwards? Also, there is no reason to use the Integer/Byte data types here. It's not going to save memory or make your code faster. Unless you specifically need to work with bytes or 16-bit integers you should long integers in vb6.
Here's you code after I fixed it:
Code:
Option Explicit
Private Const intend As Long = 4
Public Function getGenstring(intLimit As Long) As String
Dim b As Long
Dim c As Long
Dim d As Long
Dim e As Long
Dim strTemp As String
For b = 1 To intend
For c = 1 To intend
For d = 1 To intend
For e = 1 To intend
strTemp = strTemp & b & c & d & e & ","
Next e
Next d
Next c
Next b
getGenstring = strTemp
End Function
Re: How to generate string by specifies a maximum number of repeated characters?
Didn't we have a thread some days ago, in which we discussed repeating of strings?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad