Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I reverse lines of text without reversing the numbers

Status
Not open for further replies.

tango1948

Programmer
Joined
Dec 23, 2006
Messages
111
Location
IL
I need to reverse lines of text in a file without reversing the numbers eg:

input record: ABC 123 XYZ EFG

output string: GFE ZYX 123 CBA

How do I do this?

Code snippet appreciated

Thanks

David
 
I think the following should work for you. The function accepts a string and returns it reversed except for numbers. This only reverse one line at a time...Not the whole file. That might take a bit more work or this may work. Depends on the length of the file I would think, because of buffer, etc.

To use it while looping through a text file, just pass each line to the function.

Code:
    Private Function ReverseString(ByVal strText As String) As String

        ' Define Variables
        Dim strReturn As String = String.Empty
        Dim strNumbers As String = String.Empty

        ' Loop through the text, starting at the last character and working backwards
        For i As Int32 = strText.Length - 1 To 0 Step -1
            ' If the character is a number, add it to a number string
            If IsNumeric(strText.Substring(i, 1)) Then
                strNumbers = strText.Substring(i, 1).ToString & strNumbers
            Else
                ' If the chararcter is not a number, check to see if the number string is empty
                ' If not, add it plus the character and clear the number string holder
                ' Otherwise, just add the character
                If strNumbers <> String.Empty Then
                    strReturn &= strNumbers
                    strNumbers = String.Empty
                End If
                strReturn &= strText.Substring(i, 1)
            End If
        Next

        ' Put the reversed string somewhere
        Return strReturn

    End Function

Hope it helps.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top