0
CorrectHorseBatteryStaple

Correct
Horse
Battery
Staple
(Empty)

还有一个问题,我不能使用除了 Mid()、Right()、Left()、Len()、Asc() 之外的类、函数、内置函数。这使整个事情变得更加困难。

我一生都无法弄清楚如何比较字符串中的字符并以某种方式停止循环/将第一个单词存储在数组中等等。

这是我到目前为止所做的,没有任何意义:

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
4

2 回答 2

1

在开始之前,我建议您使用列表而不是数组。这样,如果您想拆分更多单词,则无需更改代码。但是,我猜您还没有涵盖这些内容。所以...

最简单的方法是循环遍历数组的每个字符,如果字符是大写的,则移动到下一个数组项并将该字符添加到数组项中。如果字符是小写,则只需将其添加到当前数组项。您不需要以这种方式使用这么多变量。

这里假设第一个字母是大写的。如果不是,将会有一个

索引超出范围

错误。


干得好 ..

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

我应该解释为什么 Z 以 -1 开头。这是基于输入字符串的第一个字母是大写的假设。

当您第一次执行循环时,存储的第一个字符temp是大写的,If并且执行了第一条语句的内容,因此将 1 添加到 z 使 z=0。然后将这个大写的第一个字母添加到数组的第一个元素 str(0) 中。

当您继续循环时,后续的小写字母只会添加到 str(0)。

当循环到达下一个大写字母时,z再次将 1 添加到 z=1 并将大写字母添加到 z(1) 等等。

于 2017-05-15T01:56:57.923 回答
1

显然,您还没有干运行代码。它充满了错误,因此它永远不会按预期运行。

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

在下一行,我认为您打算使用Mid(input, i , 1). 1不是ll会给你整个字符串,而不仅仅是一个字母。

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

这条线不会考虑Aand Z。你应该使用>=<=

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

此行将在最后一个字符上返回错误或空字符串

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

此行不会考虑数组中的第一个元素

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

看起来您已经受限于使用本机 VB6 函数的要求,尽管 VB.net 的功能可以帮助您以更少的行更简洁地编写它。

下面的代码,同样限制为 5 个字,应该给你你需要的输出:

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
于 2017-05-15T00:46:51.687 回答