dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Ok, here's the problem

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Ok, here's the problem

    [Originally posted by Mike Andreson]

    Below is the problem. Having never used VB before, till the new director said, hey, lets do everything in VB... I kinda was like, um.. ok.
    Anyways, I need help..and I need help fast.

    Using the following string

    “one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fif teen”

    Parse the string into an array where each word is an element within the array.

    Create a new array. Sort the words alphabetically then insert them into the new array.

    Create another array. This time change the spelling of each word so it is reverse the normal spelling (e.g. twelve now spelled as evlewt). Sort the new words alphabetically then insert them into the second array.

    Create a third array. Sort the words alphabetically based on the letter in the middle of the word, skipping all words that have an even number of letters (e.g. the middle letter of seven is v), then insert them into the third array. Use a dynamically sized array for this using the ReDim keyword based on the number of words that match the criteria.

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Ok, here's the problem

    [Originally posted by neophile]

    Insert?

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Ok, here's the problem

    [Originally posted by neophile]

    Here are the functions you'll need...



    Public Function TrimArray(StrArr() As String) As String()
    Dim sArr() As String
    Dim l As Long

    sArr = StrArr
    For l = 0 To UBound(sArr)
    sArr(l) = Trim(sArr(l))
    Next

    TrimArray = sArr
    Erase sArr
    End Function

    Public Function SortArray(StrArr() As String) As String()
    Dim sArr() As String
    Dim lA As Long
    Dim lB As Long
    Dim sTmp As String

    sArr = StrArr
    For lA = 0 To UBound(sArr)
    For lB = 0 To UBound(sArr)
    If lA <> lB Then
    If sArr(lA) < sArr(lB) Then
    sTmp = sArr(lA)
    sArr(lA) = sArr(lB)
    sArr(lB) = sTmp
    End If
    End If
    Next
    Next

    SortArray = sArr
    Erase sArr
    sTmp = vbNullString
    End Function

    Public Function ReverseArray(StrArr() As String) As String()
    Dim sArr() As String
    Dim l As Long

    sArr = StrArr
    For l = 0 To UBound(sArr)
    sArr(l) = Reverse(sArr(l))
    Next

    ReverseArray = sArr
    Erase sArr
    End Function

    Public Function RemoveEvensArray(StrArr() As String) As String()
    Dim sArr() As String
    Dim l As Long

    For l = 0 To UBound(StrArr)
    If IsOddLen(StrArr(l)) Then
    If l = 0 Then
    ReDim sArr(0)
    Else
    ReDim Preserve sArr(UBound(sArr) + 1)
    End If
    sArr(UBound(sArr)) = StrArr(l)
    End If
    Next

    RemoveEvensArray = sArr
    Erase sArr
    End Function

    Public Function SortMidsArray(StrArr() As String) As String()
    Dim sArr() As String
    Dim lA As Long
    Dim lB As Long
    Dim sTmp As String

    sArr = StrArr
    For lA = 0 To UBound(sArr)
    For lB = 0 To UBound(sArr)
    If lA <> lB Then
    If Middle(sArr(lA)) < Middle(sArr(lB)) Then
    sTmp = sArr(lA)
    sArr(lA) = sArr(lB)
    sArr(lB) = sTmp
    End If
    End If
    Next
    Next

    SortMidsArray = sArr
    Erase sArr
    sTmp = vbNullString
    End Function

    Public Function Reverse(ByVal Text As String) As String
    Dim i As Integer
    For i = 1 To Len(Text)
    Reverse = Mid(Text, i, 1) & Reverse
    Next
    End Function

    Public Function Middle(ByVal Text As String) As String
    Middle = Mid(Text, Int(Len(Text) / 2 + 0.5), 1)
    End Function

    Public Function IsOddLen(ByVal Text As String) As Boolean
    IsOddLen = (Len(Text) Mod 2)
    End Function



    ... Which you would use to do what you ask like this...



    Public Sub TestString(ByVal Text As String)
    Dim sText() As String
    Dim sAlpha() As String
    Dim sReverse() As String
    Dim sMiddle() As String

    sText = Split(Text, ",")
    sText = TrimArray(sText)
    sAlpha = SortArray(sText)
    sReverse = ReverseArray(sText)
    sMiddle = SortMidsArray(RemoveEvensArray(sText))
    etc.
    .
    .
    .


    I hope this helps ;)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center