How to reverse string using loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugianoor
    New Member
    • Nov 2021
    • 3

    #1

    How to reverse string using loop

    Hai, i got a problem with reverse text using loop, i have try like this


    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    Dim reverse_text As String
    reverse_text = TextBox1.Text
    For i As Integer = reverse_text .Length To 1 Step -1
    TextBox2.Text &= Mid(reverse_tex t , i, 1)
    Next

    End Sub

    The Result is
    EAT APPLE reverse to ELPPA TAE

    But that not the reverse result i want,

    How can i do to make the result look like this,

    EAT APPLE reverse to TAE ELPPA


    Pls, Help, Thanks Before
  • cactusdata
    Recognized Expert New Member
    • Aug 2007
    • 225

    #2
    Split into an array of words, reverse each item, join the words with a space.

    Comment

    • sugianoor
      New Member
      • Nov 2021
      • 3

      #3
      But how can i do that ? Im really new with vb.net

      Comment

      • SioSio
        Contributor
        • Dec 2019
        • 272

        #4
        What is the reason for using loop?
        Code:
        TextBox2.Text = StrReverse(reverse_text)

        Comment

        • cactusdata
          Recognized Expert New Member
          • Aug 2007
          • 225

          #5
          This is one method:
          Code:
          Dim plain_text As String
          Dim reverse_text As String
          
          Dim separator As Char
          Dim words() As String
          Dim item As Integer
          Dim chars() As Char
          
          plain_text = "EAT APPLE"
          separator = " "
          
          words = plain_text.Split(separator)
          For	item = 0 To words.Length - 1
          	chars = words(item).ToCharArray()	
          	Array.Reverse(chars)
          	words(item) = New String(chars)
          Next
          reverse_text = String.Join(separator, words)
          
          Console.WriteLine(plain_text)
          Console.WriteLine(reverse_text)
          Output:
          Code:
          EAT APPLE
          TAE ELPPA

          Comment

          • sugianoor
            New Member
            • Nov 2021
            • 3

            #6
            Thank You Very Much, You're The Best 👍

            Comment

            • SioSio
              Contributor
              • Dec 2019
              • 272

              #7
              I understand what you want to do.
              Code:
              		Dim i As integer
              		textBox2.Text = ""
              		Dim arr1() As String = System.Text.RegularExpressions.Regex.Split(textBox1.text, "\s")
              		For i = 0 to UBound(arr1) - 1
              			textBox2.text = textBox2.text & StrReverse(arr1(i)) & " "
              		Next i
              		textBox2.text = textBox2.text & StrReverse(arr1(i))

              Comment

              Working...