DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Senior Member
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. Senior Member
Join Date
Aug 2004
Posts
43,023

Re:Ok, here's the problem

[Originally posted by neophile]

Insert?

3. Senior Member
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
•

 FAQ Latest Articles Java .NET XML Database Enterprise