标题:VB面试题
只看楼主
moxiansun011
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2011-4-19
结帖率:60%
 问题点数:0 回复次数:9 
VB面试题
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student. a am I”。
搜索更多相关主题的帖子: 英文句子 标点符号 
2011-05-13 15:33
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
一开始学VB的我记得有在MSDN的看到或试过~有方法可以一行解决倒着取资料~
但是没用过也忘了~所以就用这样麻烦的方法~

程序代码:
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

不要選我當版主
2011-05-13 16:41
moxiansun011
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2011-4-19
得分:0 
你的答案不全面,假如输入的是另一字符串呢?
2011-05-13 18:32
吹小风
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-28
得分:0 
Private Sub Command1_Click()
Dim a As String, k As Integer, b() As String, i As Integer
a = InputBox("请输入英文")
i = 1
Do
k = InStr(a, " ")
ReDim Preserve b(i)
b(i) = Left(a, k)
a = Mid(a, k + 1)
i = i + 1
Text1 = b(i - 1) + Text1
Loop Until k <= 0
Text1 = a + " " + Text1
End Sub
2011-05-13 20:05
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:652
专家分:3402
注 册:2008-5-7
得分:0 
Private Sub Command1_Click()
Dim a As String, k As Integer, b() As String, i As Integer
a = InputBox("请输入英文")
b=split(a," ")
a=""
for i=ubound(b) to 0 step -1
  a=a & b(i) & " "
next
text1=trim(a)
End Sub
'盗用了楼上的部分代码,程序代码更少些
2011-05-13 21:44
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
有没有搞错阿~要写全面性~有奖品 = =

程序代码:
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


写到一半还当机~害我又重写~有判断的ASCII不能随便乱输入~
我寫的比你出的題目更複雜~原來是用空白切開就行~我把關鍵字以前的字符全部倒過來寫~
我程序可以判断你拿来比对的是数字或是字符~各有各的处理方式~

[ 本帖最后由 wube 于 2011-5-13 23:02 编辑 ]

不要選我當版主
2011-05-13 22:54
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
用空白切数组比用字符串接简单多了~我都是用 This is a book ! 来测试的~

程序代码:
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

不要選我當版主
2011-05-13 23:28
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
Sorry~我认真了= =

不要選我當版主
2011-05-13 23:34
syx54
Rank: 2
等 级:论坛游民
帖 子:11
专家分:18
注 册:2008-8-12
得分:0 
方法:使用动态数组。
思路:
1.单词之间用空格分隔的,所以逐个取出单词,存放到数组里。
2.将数组逆序输出就可以了。

2011-05-15 21:13
bczgvip
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:66
帖 子:1310
专家分:5312
注 册:2009-2-26
得分:0 
程序代码:
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
2011-05-17 03:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-339732-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051124 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved