469,612 Members | 1,650 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

How to reverse string using loop

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


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 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_text , 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
4 Weeks Ago #1

✓ answered by cactusdata

This is one method:
Expand|Select|Wrap|Line Numbers
  1. Dim plain_text As String
  2. Dim reverse_text As String
  3.  
  4. Dim separator As Char
  5. Dim words() As String
  6. Dim item As Integer
  7. Dim chars() As Char
  8.  
  9. plain_text = "EAT APPLE"
  10. separator = " "
  11.  
  12. words = plain_text.Split(separator)
  13. For    item = 0 To words.Length - 1
  14.     chars = words(item).ToCharArray()    
  15.     Array.Reverse(chars)
  16.     words(item) = New String(chars)
  17. Next
  18. reverse_text = String.Join(separator, words)
  19.  
  20. Console.WriteLine(plain_text)
  21. Console.WriteLine(reverse_text)
Output:
Expand|Select|Wrap|Line Numbers
  1. EAT APPLE
  2. TAE ELPPA

6 13476
cactusdata
184 Expert 128KB
Split into an array of words, reverse each item, join the words with a space.
4 Weeks Ago #2
sugianoor
3 2Bits
But how can i do that ? Im really new with vb.net
4 Weeks Ago #3
SioSio
264 256MB
What is the reason for using loop?
Expand|Select|Wrap|Line Numbers
  1. TextBox2.Text = StrReverse(reverse_text)
4 Weeks Ago #4
cactusdata
184 Expert 128KB
This is one method:
Expand|Select|Wrap|Line Numbers
  1. Dim plain_text As String
  2. Dim reverse_text As String
  3.  
  4. Dim separator As Char
  5. Dim words() As String
  6. Dim item As Integer
  7. Dim chars() As Char
  8.  
  9. plain_text = "EAT APPLE"
  10. separator = " "
  11.  
  12. words = plain_text.Split(separator)
  13. For    item = 0 To words.Length - 1
  14.     chars = words(item).ToCharArray()    
  15.     Array.Reverse(chars)
  16.     words(item) = New String(chars)
  17. Next
  18. reverse_text = String.Join(separator, words)
  19.  
  20. Console.WriteLine(plain_text)
  21. Console.WriteLine(reverse_text)
Output:
Expand|Select|Wrap|Line Numbers
  1. EAT APPLE
  2. TAE ELPPA
4 Weeks Ago #5
sugianoor
3 2Bits
Thank You Very Much, You're The Best 👍
4 Weeks Ago #6
SioSio
264 256MB
I understand what you want to do.
Expand|Select|Wrap|Line Numbers
  1.         Dim i As integer
  2.         textBox2.Text = ""
  3.         Dim arr1() As String = System.Text.RegularExpressions.Regex.Split(textBox1.text, "\s")
  4.         For i = 0 to UBound(arr1) - 1
  5.             textBox2.text = textBox2.text & StrReverse(arr1(i)) & " "
  6.         Next i
  7.         textBox2.text = textBox2.text & StrReverse(arr1(i))
2 Weeks Ago #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Jerry | last post: by
3 posts views Thread by minguskhan | last post: by
1 post views Thread by python101 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.