是一个输入字符串然后求最长的单词的过程,代码如下:
Public Function longest(lw as string)
dim word1 as string,letter as string,s as string
dim ll as integer ,lent as integer ,jsq as integer
s=txt1.text+""
lent=len(s)
jsq=0
ll=0
lw=""
word1=""
for i=1 to lent
letter=mid(s,i,1)
if letter <>"" then
word1=word1+letter
jsq=jsq+1
elseif jsq>ll then
ll=jsq:lw=word1:word=""
jsq=0
else
jsq=0:word1=""
end if
next i
longstw=lw
End Function
这些代码有些看不懂,不明白这个过程!
还有就是jsq>ll这个地方是不是比字母大小啊?这个有意义吗?
还有这个字母是怎么比的啊?
Public Function longest(lw as string)
dim word1 as string,letter as string,s as string
dim ll as integer ,lent as integer ,jsq as integer
s=txt1.text+""
lent=len(s)
jsq=0
ll=0
lw=""
word1=""
for i=1 to lent
letter=mid(s,i,1)
if letter <>"" then
word1=word1+letter
jsq=jsq+1
elseif jsq>ll then
ll=jsq:lw=word1:word=""
jsq=0
else
jsq=0:word1=""
end if
next i
longstw=lw
End Function
这些代码有些看不懂,不明白这个过程!
还有就是jsq>ll这个地方是不是比字母大小啊?这个有意义吗?
还有这个字母是怎么比的啊?
这个定义怎么能得出字母比大小的结论呢你把代码放到工程里面逐行运行,结论自然就出来了
Private Function longest(ByVal s As String) As String() '返回数组的原因为,字串中可能有两个以上的单词长度均为最大
Dim arr() As String
arr = Split(s, " ")
Dim tmp() As String
Dim i As Long, j As Long, Maxlen As Long
For i = 0 To UBound(arr)
If Len(arr(i)) > Maxlen Then
Maxlen = Len(arr(i))
End If
Next
For i = 0 To UBound(arr)
If Len(arr(i)) = Maxlen Then
ReDim Preserve tmp(j)
tmp(j) = arr(i)
j = j + 1
End If
Next
longest = tmp
End Function
'调用
Private Sub Command1_Click()
Dim arr() As String
Dim s As String
s = "hello this is world"
arr = longest(s)
Dim i As Long
For i = 0 To UBound(arr)
Debug.Print arr(i)
Next
End Sub
Dim word1 As String, letter As String, s As String
Dim ll As Integer, lent As Integer, jsq As Integer
s = Text1.Text + ""
lent = Len(s)
jsq = 0
ll = 0
lw = ""
word1 = ""
For i = 1 To lent
letter = Mid(s, i, 1)
If letter <> " " Then
word1 = word1 + letter
jsq = jsq + 1
ElseIf jsq > ll Then
ll = jsq: lw = word1: word = ""
jsq = 0
Else
jsq = 0: word1 = ""
End If
Next i
If jsq > ll Then
ll = jsq:
lw = word1
End If
longstw = lw
End Function'这个过程的意思是
'比如输入一个字符串 "what are you doing now?"
'得出返回值 doing(最长的单词)
'里面是根据 If letter <> " " Then 来判断是否是一个单词的结束或开始的
'word1存放当前单词,jsq存放当前长度 ll存放前面最长单词长度,lw存放前面最长单词
'jsq > ll 是一个比较, 就是当前长度长于最长长度时则修改最长单词及长度
'最后要加上
If jsq > ll Then
ll = jsq:
lw = word1
End If
'以完成最后一单词的判断
'这个函数里还没有加上对标点符号的处理
'可以在开始的时候就replace一下
Dim sWord As String, sLetter As String, sResult As String
Dim iLoop As Integer
lw = lw & " "
sResult = ""
sWord = ""
For iLoop = 1 To Len(lw)
sLetter = Mid(lw, iLoop, 1)
If sLetter <> " " Then
sWord = sWord + sLetter
Else
If Len(sWord) > Len(sResult) Then sResult = sWord
sWord = ""
End If
Next iLoop
longest = sResult
End FunctionPrivate Sub Form_Load()
Debug.Print longest("Hello Welcome Hi!")
End
End Sub另外,这个函数并不能正确处理标点符号的问题,它只把空格作为单词之间的分隔符号。
Public Function longest(lw as string)
dim word1 as string,letter as string,s as string
dim ll as integer ,lent as integer ,jsq as integer
s=txt1.text+"" '从txt1取得句子
lent=len(s) '取得句子长度
jsq=0 '当前单词长度
ll=0 '最大单词长度
lw="" '最长单词
word1="" '当前单词
for i=1 to lent '循环扫描
letter=mid(s,i,1) '取一个字符
if letter <>" " then '如果不是空格
word1=word1+letter '当前单词加上此字符
jsq=jsq+1 '当前单词长度加1
elseif jsq>ll then '是空格且当前单词长于最长单词
ll=jsq:lw=word1:word=""
jsq=0
else '是空格,当前单词不长于最长单词
jsq=0:word1=""
end if
next i
longstw=lw '将参数赋给函数返回值?似多余。
End Function这段代码的算法和风格都很差,写一段给你看看:Public Function LongestWord (Byval strText As String) As StringDim strWords() As String, i As Integer
Dim intLength As Integer, intMax As Integer strWords = Split(strText, Space(1)) '以空格为界切分成数组
For i = 0 To Ubound(strWords)
intLength = Len(strWords(i)) '逐项比较数组成员长度
If intLength > intMax Then
intMax = intLength
LongestWord = strWords(i)
End If
Next iEnd Function