简体   繁体   中英

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

Clearly, you haven't dry run the code. It's littered with mistakes because of which it will never run as intended.

Dim str(5) As String
For z As Integer = 1 To 5  ' will never run for over 5 words

on this next line, I think you meant to use Mid(input, i , 1) . 1 not l . l will give you the whole string and not just a single letter.

        temp = Mid(input, i, l)
        temp2 = Mid(input, i + 1, l)

This line won't consider A and Z . you should use >= and <=

        If Asc(temp) >= 65 And Asc(temp) <= 90 Then

This line will return an error or an empty string on the last character

temp2 = Mid(input, i + 1, l)

This line won't consider the first element in the array

For a As Integer = 1 To 5
    Console.WriteLine(str(a))
Next a

It looks like you've been limited by your requirement to using native VB6 functions, although VB.net's functionality would help you to write this more cleanly and in fewer lines.

The below code, again limited to 5 words, should give you the output you need:

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
    Dim arrCounnter As Integer
    Console.WriteLine("Enter the string: ")
    input = Console.ReadLine()
    tempstr = ""
    l = Len(input)

    For i As Integer = 1 To l
        temp = Mid(input, i, 1)
        'If capital, add to new temp; put old temp in array
        If Asc(temp) >= 65 And Asc(temp) <= 90 Then
            If tempstr <> "" Then
                str(arrCounnter) = tempstr
                arrCounnter = arrCounnter + 1
            End If
            tempstr = temp
        Else
            'If not, add to old temp, nxt
            tempstr = tempstr & temp
        End If
        If i = l Then str(arrCounnter) = tempstr
    Next i

    For a As Integer = 0 To 5
        If str(a) = "" Then
            Console.WriteLine("(Empty)")
        Else
            Console.WriteLine(str(a))
        End If
    Next a
    Console.ReadKey()
End Sub

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM