Search code examples
arraysvb.netpascalcasing

Having trouble splitting a PascalCase string and storing it in an array in VB


CorrectHorseBatteryStaple

to

Correct
Horse
Battery
Staple
(Empty)

There's also a catch, I can't use classes, functions, built-in functions other than Mid(), Right(), Left(), Len(), Asc(). Which makes the whole thing much more difficult.

I can't for the life of me figure out how to compare the characters in the string and somehow stop the loop/store the first word in the array and so on.

Here's what I've done so far, which isn't anything meaningful:

Sub Main()
    Dim input As String
    Dim str(5) As String
    Dim tempstr As String
    Dim temp As Char
    Dim temp2 As Char
    Dim l As Integer
    Console.WriteLine("Enter the string: ")
    input = Console.ReadLine()
    l = Len(input)
    For z As Integer = 1 To 5
        For i As Integer = 1 To l
            temp = Mid(input, i, l)
            temp2 = Mid(input, i + 1, l)
            If Asc(temp) > 65 And Asc(temp) < 90 Then
                tempstr = temp
                If Asc(temp2) > 65 And Asc(temp2) < 90 Then
                    tempstr = temp
                Else
                    tempstr = tempstr & temp
                End If
            Else
                tempstr = tempstr & temp
            End If
        Next i
        str(z) = tempstr
    Next z
    For a As Integer = 1 To 5
        Console.WriteLine(str(a))
    Next a
    Console.ReadKey()
End Sub

Solution

  • Before I start, I would suggest that you use a list instead of an array. That way, if you want to split more words, you wont need to change the code. However, I'm guessing you haven't covered those yet. So...

    The simplest way would be to loop through each character of the array and if the character is upper case then move on to the next array item and add that character to the array item. If the character is lower case then just add it to the current array item. You don't need to use so many variables this way.

    There is an assumption here that the first letter will be upper case. if it isn't there will be an

    Index out of range

    error.


    Here you go ..

    Module module1
        Sub Main()
    
            Dim input As String
            Dim str(3) As String
            Dim temp As String
            Dim l As Integer
            Dim z As Integer = -1 ' array index
            Console.WriteLine("Enter the string: ")
            input = Console.ReadLine()
            l = Len(input)
            For i As Integer = 1 To l
                temp = Mid(input, i, 1)
                'if temp is a capital letter increase the array index by 1 and add temp to that array item
                If (Asc(temp) >= 65 And Asc(temp) <= 90) Then
                    z = z + 1
                    str(z) = str(z) & temp
                End If
                ' if the temp is lower case then just add temp to the current array item
                If (Asc(temp) >= 97 And Asc(temp) <= 122) Then
                    str(z) = str(z) & temp
                End If
            Next
            Console.WriteLine()
    
            For a As Integer = 0 To 3
                Console.WriteLine(str(a))
            Next a
            Console.ReadKey()
        End Sub
    End Module
    

    I should explain why Z starts out as -1. This is based on the assumption that the first letter of the input string is upper case.

    As you go through the loop for the first time, the first character that got stored in temp is upper case and the contents of the first If statement are executed, so 1 is added to z making z=0. Then this first letter which is upper case is added to str(0) which is the first element of the array.

    As you carry on through the loop, the subsequent lower case letters merely get added to str(0).

    When the loop reaches the next upper case letter, 1 is added to z again so that z=1 and the upper case letter is added to z(1) and so on.