3 Replies - 3630 Views - Last Post: 30 March 2011 - 12:15 PM Rate Topic: -----

#1 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Using a stack to reverse word order

Posted 30 March 2011 - 04:01 AM

Hi

I'm trying to use a stack object to reverse the order of words (not letters) in a sentence. I'm fairly sure that the split and push methods are working, but I can only get the first word (i.e. the first word in the original sentence), to be displayed.

I have the following code:
    Private Sub btnReverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReverse.Click

        Dim s As New Stack
        Dim sentence, word, reverse As String
        Dim words As Array

        sentence = txtOriginalOrder.Text

        words = sentence.Split(" ")

        For Each word In words
            s.Push(word)
        Next

        Do
            txtReverseOrder.Text = s.Pop & " "
        Loop Until s.Count = 0
End Sub


I think the Do loop is the problem, but nothing I've tried makes any different output.
Anyone know how I can fix this?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Using a stack to reverse word order

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Using a stack to reverse word order

Posted 30 March 2011 - 04:16 AM

Stack is LIFO (Last in First Out) so when you do your For each word in Words you're adding the first word from your split string and the next word is put in before it. In other words think of a container that holds layers of cardboard. When you put the first one in the next one pushes it on down the stack. This means that in order to get the first one out you have to remove everything above it. Since you are pushing your words into S in order you have reversed them for a forward iteration through the collection.

So... Instead of your Do Loop you should just be able to use
For each word in S '<---Notice you're now looking in your stack that you filled 
  TxtReverseOrder.Tet &= word & " "
Next


EDIT:
Forgot to mention. Is this an exercise in using a stack or is this your solution to reversing a string by words because you can reverse the array and get the same result

Array.Reverse(Words)
For each word in Words
...
Next

This post has been edited by CharlieMay: 30 March 2011 - 04:25 AM

Was This Post Helpful? 1
  • +
  • -

#3 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Re: Using a stack to reverse word order

Posted 30 March 2011 - 11:39 AM

Thanks for the help, CharlieMay.

It's an exercise simply to use a stack. Turns out that the algorithm was correct, but the reason I was getting only one word in the output was because I was missing one ampersand symbol. As you indicated, modifying the following line worked:
txtReverseOrder.Text &= s.Pop & " "


Cheers! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: Using a stack to reverse word order

Posted 30 March 2011 - 12:15 PM

If this is something you will do a lot of consider using StringBuilder instead of &...

        'some test words already split
        Dim ipsumA() As String = New String() {"Lorem", "ipsum", "dolor", "sit", _
                                               "amet", "consectetur", "adipisicing", _
                                               "sint", "occaecat", "cupidatat", _
                                               "sunt", "in", "culpa", "qui", _
                                               "mollit", "anim", "laborum"}

        Dim s As New Stack(Of String)

        For x As Integer = 0 To ipsumA.Length - 1
            s.Push(ipsumA(x))
        Next

        Dim sb As New System.Text.StringBuilder

        For x As Integer = 0 To s.Count - 1
            sb.Append(s.Pop)
            sb.Append(" ")
        Next
        txtReverseOrder.Text = sb.ToString


This post has been edited by dbasnett: 30 March 2011 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1