情报员小王在工作时发现R国会用一些对称字符(如ABBA、ABA、123321)进行通信。R国为防止通信内容被破解,会在对称字符的头、尾加一些无关字符,如:12ABBA、ABAKK、5123432198。因此小王编写了一个VB程序,用于读取通信字符串中最长的对称字符。在文本框Text1中输入通信字符,单击“读取”按钮 Command1,在标签Label2中显示最长的对称字符,程序运行界面如图所示。请完善代码。
Private Sub Command1_Click()
Dim x As String, n As Integer, i As Integer
Dim k As Integer, start As Integer
Dim max As Integer
x = Text1.Text: n = Len(x)
max = 0: start = 1
For i = 1 To n
k = n
Do While k > i
IfAnd k - i + 1 > max Then
max = k - i + 1
start=i
End If
Loop
Next i
Label2.Caption =“最长回文字符串为” + Mid(x, start, max)
End Sub
Function isPa(t As String) As Boolean ’判断是不是回文数
Dim i As Integer
For i = 1 To Len(t) \ 2
If Mid(t, i, 1) <> Mid(t, Len(t) - i + 1, 1) Then isPa = False
Next i
End Function
答案:【1】isPa(Mid(x,i,k-i+1))【2】k=k-1【3】isPa = True