Results 1 to 2 of 2

Thread: Help with message encryption task, using an array

  1. #1
    DerpBane is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2013
    Posts
    4
    Reputation
    0

    Angry Help with message encryption task, using an array

    Hi,
    So I've been trying to get through this task for awhile now, for my college work, and there's something that I'm not getting. Here is the task:
    Write a program that declares an array of characters with six rows and six columns and fills it with ?space? characters.
    It should then input a message, place it letter by letter into the array by rows, then output the result of reading it column by column.
    When testing your program try to use messages that are close to the maximum length or your encrypted version will contain many blanks.
    When copying down the encrypted version, count the spaces carefully.
    Now here is my attempt, please forgive how ugly it looks, but it is the result of a lot of changing and testing:
    Module Module1


    Sub Main()
    Dim Message(5, 5) As String
    Dim number1 As Integer
    Dim number As Integer
    Dim StringLine, EncryptedString As String
    For Index2 = 0 To 5
    For Index = 0 To 5
    Message(Index, Index2) = ""


    Next Index


    Next Index2


    Console.Write("Insert a message <up to 36 characters>: ")
    StringLine = Console.ReadLine


    Dim Testnum As Integer


    Testnum = 36 - StringLine.Length
    If Testnum > 0 Then
    For num = 0 To Testnum
    StringLine = StringLine + (" ")
    Next num


    End If


    number = -1
    For Index2 = 0 To 5
    number = number + 1
    For Index = 0 To 5
    number1 = number
    Message(Index2, Index) = StringLine.Substring(number1)
    number1 = number1 + 1
    Next Index
    number = number1
    Next Index2


    EncryptedString = ""
    For Index2 = 0 To 5
    For Index = 0 To 5
    EncryptedString = EncryptedString + Message(Index, Index2)
    Next Index
    Next Index2




    Console.WriteLine(EncryptedString)
    Console.ReadLine()
    End Sub


    End Module





    When I try to run it, I usually get greeted by an encrypted message that is far longer than my starting message. Technically, I can move on from this task, but it's annoying me.

    thanks
    Last edited by JohnH; 12-14-2013 at 4:38 PM. Reason: formatting boxes

  2. #2
    IanRyder is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2012
    Location
    Healing, NE Lincs, UK
    Posts
    1,128
    Reputation
    743
    Hi,

    I can see a few issues here but there are two main things that are causing you your problems. These are:-

    1) The first issue is that you have declared your array as a String Array so you are able to store more that one character in each element of the array. I would suggest that you change this to an array of Char and then turn Option Strict On. This will then point you to two assignment errors in your code which you can solve using the CChar Function.

    2) The second issue is your use of Substring. You are using the first Overload of the Method that returns ALL the characters of String from a Starting Point. You need to be using the second overload of that method which also takes an argument specifying the number of characters to be returned from the starting point. Have a look here:-

    String.Substring Method (System)

    Hope that helps.

    Cheers,

    Ian

Tags for this Thread

Bookmarks

Posting Permissions

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