编写程序VB,在一个文本框中输入一个简单的英文句子,找出这个英文句子中最长的单词

编写程序VB,在一个文本框中输入一个简单的英文句子,找出这个英文句子中最长的单词
Private Sub Command1_Click()
Dim word() As String,s As String
Dim k As Integer,maxw As String,n As Integer
s = Text1.Text
Do
n = InStr(s," ")
If n 0 Then
k = k + 1
ReDim Preserve word(k)
word(k) = Left(s,n - 1)
List1.AddItem word(k)
s = Right(s,n + 1)
End If
Loop Until n = 0
ReDim Preserve word(k + 1)
word(k + 1) = Left(s,Len(s) - 1)
List1.AddItem word(k + 1)
maxw = word(1)
For n = 2 To UBound(word)
If Len(word(n)) > Len(word(1)) Then
maxw = word(n)
End If
Next n
Text2 = maxw
End Sub
提示为溢出,k=k+1 变黄色
其他人气:866 ℃时间:2020-05-10 09:09:08
优质解答
Private Sub Command1_Click()
If Text1.Text = "" Then Exit Sub
Dim word As Variant,s As String
Dim k As Integer,maxw As String,n As Integer
s = Replace(Replace(Text1.Text,","," "),"."," ")
s = Replace(Replace(s,","," "),"."," ")
s = Replace(s,vbCrLf," ")
s = Trim(Replace(s," "," "))
If s = "" Then Exit Sub
word = Split(s," ")
n = UBound(word)
For k = 0 To n
If n = k Then Exit For
If Len(CStr(word(k + 1))) > Len(maxw) Then
maxw = word(k + 1)
End If
Next
MsgBox maxw & "=" & Len(maxw) & "字节"
End Sub我要改我追问的。。。谢谢哦,我看一下,就怕看别人代码测试了,没有看到问题,你是在什么情况下有的提示错?经过空测试,错误:应该在 s = Text1.Text 下面判断:if s="" then exit sub
我来回答
类似推荐
请使用1024x768 IE6.0或更高版本浏览器浏览本站点,以保证最佳阅读效果。本页提供作业小助手,一起搜作业以及作业好帮手最新版!
版权所有 CopyRight © 2012-2024 作业小助手 All Rights Reserved. 手机版