VB面试题
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student. a am I”。
Private Sub Form_Load() Dim Temp As String, Temp1 As String, Temp2 As String Temp = "I am a student." Temp1 = Mid(Temp, InStr(Temp, "student"), Len(Temp) - InStr(Temp, "student") + 1) For i = InStr(Temp, "student") To 1 Step -1 Temp2 = Temp2 & Mid(Temp, i, 1) Next i MsgBox Temp1 & Temp2 End Sub
Option Explicit Private Sub Form_Load() Dim Source() As String, TempString As String Dim Num As Variant, LenNum As Integer, NewString As String Dim i As Integer, iCount As Integer TempString = Trim(InputBox("Input String ?")) ReDim Source(Len(TempString) - 1) For i = 0 To UBound(Source) Source(i) = Mid(TempString, i + 1, 1) Next i Num = Trim(InputBox("What Index ?")) If IsNumeric(Num) = True Then For i = 0 To UBound(Source) NewString = NewString & Source(i) Next i If Num < Len(TempString) Then For i = Num - 1 To 1 Step -1 NewString = NewString & Source(i) Next i End If ElseIf CheckNum(CStr(Num), iCount) = True Then LenNum = InStr(TempString, Num) If LenNum < Len(TempString) Then NewString = Num & Mid(TempString, LenNum + iCount, Len(TempString) - LenNum + iCount) For i = Len(TempString) - Len(NewString) To 1 Step -1 NewString = NewString & Mid(TempString, i, 1) Next i ElseIf LenNum = Len(TempString) Then NewString = TempString Else MsgBox "Error !" Exit Sub End If End If MsgBox NewString End Sub Private Function CheckNum(Num As String, iCount As Integer) As Boolean Dim i As Integer CheckNum = False: iCount = 0 For i = 0 To Len(Num) - 1 If (Asc(Mid(Num, i + 1, 1)) > 47 And Asc(Mid(Num, i + 1, 1)) < 58) Or (Asc(Mid(Num, i + 1, 1)) > 64 And Asc(Mid(Num, i + 1, 1)) < 91) Or (Asc(Mid(Num, i + 1, 1)) > 96 And Asc(Mid(Num, i + 1, 1)) < 123) Or (Asc(Mid(Num, i + 1, 1)) = 32) Then CheckNum = True iCount = iCount + 1 Else CheckNum = False: Exit For End If Next i End Function
Option Explicit Private Sub Form_Load() Dim TempString As String, CodeString1() As String Dim Temp As String, TempNum As Integer, NewString As String Dim i As Integer, j As Integer TempString = Trim(InputBox("Input?")) CodeString1 = Split(TempString, " ") Temp = Trim(InputBox("Compare?")) For i = 0 To UBound(CodeString1) If InStr(CodeString1(i), Temp) <> 0 Then TempNum = InStr(CodeString1(i), Temp) End If Next i For i = TempNum To UBound(CodeString1) NewString = NewString & " " & CodeString1(i) If i = UBound(CodeString1) Then For j = TempNum - 1 To 0 Step -1 NewString = NewString & " " & CodeString1(j) Next j End If Next i MsgBox NewString End Sub
Option Explicit Private Sub Form_Load() Dim Temp As String Dim Temp1 As String Dim aStr() As String Dim i&, n& Temp = "I am a student." 'Temp = "This is a book !" aStr = Split(Temp, " ") Temp1 = Space$(Len(Temp)) n = 1 For i = UBound(aStr) To LBound(aStr) Step -1 Mid$(Temp1, n) = aStr(i) n = n + Len(aStr(i)) + 1 Next Debug.Print Temp1 End Sub