Results 1 to 7 of 7

Thread: How to generate string by specifies a maximum number of repeated characters?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    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

    If X=getGenstr(4)

    Result will be
    1111
    1112
    1113
    1114
    1122
    1123
    1124
    1133
    1134
    1144
    1222
    1223
    1224
    1233
    1234
    1244
    1333
    1334
    1344
    1444
    2222
    2223
    2224
    2233
    2234
    2244
    2333
    2334
    2344
    2444
    3333
    3334
    3344
    3444
    4444

    Thank you for all posts.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to generate string by specifies a maximum number of repeated characters?

    Quote Originally Posted by Peter Swinkels View Post
    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
    Thank you very much for you suggestion.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to generate string by specifies a maximum number of repeated characters?

    After trying it. Is this a good way?
    Attached Files Attached Files
    Last edited by standardusr; Jun 13th, 2021 at 02:41 AM.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How to generate string by specifies a maximum number of repeated characters?

    Your program appears to work. What is it for? You're not supposed to include *.exe files in your attachments btw.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to generate string by specifies a maximum number of repeated characters?

    Just a little skeptical. Because some tasks are looping too many repetitions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width