3 Replies - 545 Views - Last Post: 03 November 2015 - 06:24 PM Rate Topic: -----

#1 jackpeculier   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 15

How to shift elements forwards in an array in a for loop

Posted 03 November 2015 - 06:44 AM

So I'm writing an encryption program, and I'm putting encrypted characters into an array. However, every 5 characters, there needs to be a space separating the characters. I have the spaces every 5 characters, but, of course it overwrites the characters I want to display. How do I shift all the characters along 1 in the array every time a space is put in-between 5 characters? Here's my code:


For i = 0 To textcontent.Length Step 6
            intEncryptarray(i) = 32
        Next


        For x = 0 To Len(textcontent) - 1
            If intEncryptarray(x) = 32 Then

            End If
            If intEncryptarray(x) <> 32 Then ' If the currently modifiable ASCII code corresponds to space, donít encrypt it
                intEncryptarray(x) = Asc(encryptArray(x))
                intEncryptarray(x) = intEncryptarray(x) + offsetNumber ' Encrypting the character
                If intEncryptarray(x) > 126 Then ' number has to be valid ASCII number, lower it if it's too high
                    intEncryptarray(x) = intEncryptarray(x) - 64
                End If
            End If

                charIntEncryptArray(x) = Chr(intEncryptarray(x)) 'turn the number into a character
                encryptedstring = encryptedstring + charIntEncryptArray(x)

        Next


Is This A Good Question/Topic? 0
  • +

Replies To: How to shift elements forwards in an array in a for loop

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,460
  • Joined: 12-June 08

Re: How to shift elements forwards in an array in a for loop

Posted 03 November 2015 - 07:49 AM

I would simply go with copying the data to another array.. insert five into the new array, insert a space, insert five more, insert a space, etc.. Traverse the original array as per normal... track the second array's "current".
Was This Post Helpful? 1
  • +
  • -

#3 jackpeculier   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 15

Re: How to shift elements forwards in an array in a for loop

Posted 03 November 2015 - 10:27 AM

View Postmodi123_1, on 03 November 2015 - 07:49 AM, said:

I would simply go with copying the data to another array.. insert five into the new array, insert a space, insert five more, insert a space, etc.. Traverse the original array as per normal... track the second array's "current".


That's a great idea o.o Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1499
  • View blog
  • Posts: 3,797
  • Joined: 01-February 13

Re: How to shift elements forwards in an array in a for loop

Posted 03 November 2015 - 06:24 PM

You could use a List(Of Integer) instead of an Array. Then, after adding the ASCII values to it you can just iterate through the List with a For Next loop stepping 5 indexes at a time and use the List.Insert method to insert the spaces.

This post has been edited by IronRazer: 03 November 2015 - 06:25 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1