Results 1 to 11 of 11

Thread: Reverse line

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Colorado
    Posts
    23

    Arrow

    Can anyone help with the code for this command button? Thanks.

    Add a "Reverse Line" button. Reverse Line should take an entry such as

    "This is some text"

    and reverse it as follows:

    "text some is This".


  2. #2
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    Hi,

    Try NewText.Text = ReverseString(Text1.Text)


    Regards

  3. #3
    Member
    Join Date
    May 2000
    Location
    Canada
    Posts
    52
    You forgot to supply your ReverseString function. VB does not have this function built in.

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    Hi, The ReverseStr function is available in VB, but are you using VB 6.0?

    By the way, the ReverseStr may not return what you looking for, because it will return the folloing result:

    Source string = "This is some text"
    Retrun string = "txet emos si sihT"

    So, I have wrote a simple ReverseStr function by using the VBScript 5.0 or higher DLL and the code is show below:

    Code:
    Private Function ReverseStr(ByVal sourceStr As String) As String
    Dim regex As RegExp
    Dim col As MatchCollection
    Dim match As match
    Dim str() As String
    Dim result As String
    Dim length As Long
    Dim Idx As Integer
    
    '***************************************************************
    '   This example code need the VBScript.dll file
    '   Before you use this code make sure you have
    '   this DLL in your Windows system folder.
    '
    '   You can download this DLL from the following URL
    'http://www.microsoft.com/msdownload/vbscript/scripting51.asp
    '
    '***************************************************************
    
    Set regex = New RegExp
    With regex
        .IgnoreCase = True
        .Global = True
        '\S* search for all non white space charater
        '\s  search for all space character
        .Pattern = "\S*\s"
        Set col = .Execute(sourceStr)
    End With
        
    Idx = 0
    length = 0
    For Each match In col
        ReDim Preserve str(Idx)
        str(Idx) = match.Value
        length = length + match.length
        Idx = Idx + 1
    Next
    
    'get the last string
    If length <> Len(sourceStr) Then
        ReDim Preserve str(Idx)
        str(Idx) = Mid(sourceStr, length) & Chr(32)
    End If
    
    For Idx = UBound(str) To 0 Step -1
        result = result & str(Idx)
    Next
    
    If result <> "" Then MsgBox result
        
    Set regex = Nothing
    End Function

  5. #5
    Guest
    If I'm not correct, shouldn't it be StrReverse rather than ReverseStr?

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Talking

    Ya, Magatron you're rite the function in VB is StrReverse, Sorry for my typing mistake.

  7. #7
    Guest
    Similar to the one above, but a bit different:

    Code:
    Private Sub Command1_Click()
    
        Dim vArray As Variant
        Dim strArray As String
        Dim iArray As Integer
        
        vArray = Split("This is some text", " ")
        For iArray = 0 To UBound(vArray)
            strArray = strArray & Chr(32) & StrReverse(vArray(iArray))
        Next iArray
        MsgBox StrReverse(strArray)
        
    End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Colorado
    Posts
    23
    Will any of these codings work when the string is taken from a user-entered text box? I need to make it work so that if a user enters some text in a text box and then clicks on a reverse line command button it reverses the string. Should any of the above codings be modified? Thanks in advance.

  9. #9
    Guest
    All code above will work, just find where it is used and change it to the textbox (Text1).

    Code:
    Private Sub Command1_Click()
    
        Dim vArray As Variant
        Dim strArray As String
        Dim iArray As Integer
        
        vArray = Split(Text1.text, " ")
        For iArray = 0 To UBound(vArray)
            strArray = strArray & Chr(32) & StrReverse(vArray(iArray))
        Next iArray
        MsgBox StrReverse(strArray)
        
    End Sub
    Or:

    Code:
    Private Sub Command1_Click()
        Dim x As Integer
        Dim intLow As Integer
        Dim intHigh As Integer
        Dim strInput As String
        Dim strTest() As String
        Dim strNew As String
        
        strInput = Text1.text
        strTest = Split(strInput)
        intLow = LBound(strTest)
        intHigh = UBound(strTest)
        
        For x = intHigh To intLow Step -1
            strNew = strNew & strTest(x) & " "
        Next x
        
        MsgBox "Original String = " & strInput & vbCrLf & _
               "Reverse String  = " & strNew
    End Sub
    Or, I guess sourceStr would be Text1.text:

    Code:
    Private Function ReverseStr(ByVal sourceStr As String) As String
    Dim regex As RegExp
    Dim col As MatchCollection
    Dim match As match
    Dim str() As String
    Dim result As String
    Dim length As Long
    Dim Idx As Integer
    
    '***************************************************************
    '   This example code need the VBScript.dll file
    '   Before you use this code make sure you have
    '   this DLL in your Windows system folder.
    '
    '   You can download this DLL from the following URL
    'http://www.microsoft.com/msdownload/vbscript/scripting51.asp
    '
    '***************************************************************
    
    Set regex = New RegExp
    With regex
        .IgnoreCase = True
        .Global = True
        '\S* search for all non white space charater
        '\s  search for all space character
        .Pattern = "\S*\s"
        Set col = .Execute(sourceStr)
    End With
        
    Idx = 0
    length = 0
    For Each match In col
        ReDim Preserve str(Idx)
        str(Idx) = match.Value
        length = length + match.length
        Idx = Idx + 1
    Next
    
    'get the last string
    If length <> Len(sourceStr) Then
        ReDim Preserve str(Idx)
        str(Idx) = Mid(sourceStr, length) & Chr(32)
    End If
    
    For Idx = UBound(str) To 0 Step -1
        result = result & str(Idx)
    Next
    
    If result <> "" Then MsgBox result
        
    Set regex = Nothing
    End Function

  10. #10
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    Hi,
    Opssss.......Sorry !!!!!! I forgot The Function.
    But With All GREAT samples that u have now - i dont think u need my poor sample - But here it is.

    MyReversText.text = ReverseString(Text1.Text)

    Private Function ReverseString(strSource As String) As String
    '
    Dim pos As Integer 'position
    Dim strDummy As String 'dummy string
    Dim intC As Integer 'counter
    Const Space As String = " " 'space
    '
    pos = Len(strSource)
    strDummy = ""
    For intC = Len(strSource) To 1 Step -1
    If Mid(strSource, intC, 1) = Space Then
    strDummy = strDummy & Mid(strSource, intC, pos - intC + 1)
    pos = intC
    End If
    Next intC
    strDummy = strDummy & Left(strSource, pos - intC)
    ReverseString = Right(strDummy, Len(strDummy) - 1)
    '
    End Function


    Regards

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Might be handy if you want some speed
    Code:
    Function fastStrReverse(text As String) As String
    Dim a() As Byte, b() As Byte, c As Long, n As Long
        a = StrConv(text, vbFromUnicode)
        c = UBound(a)
        ReDim b(c)
        For n = 0 To c
            b(c - n) = a(n)
        Next n
        fastStrReverse = StrConv(b, vbUnicode)
    End Function
    Fast as you can get if you have vb5, but in vb6 the built in is probably faster
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width