Hey guys!
How can I invert (or reverse, whatever) a string, but reversing each word?
Like: This is a string.
Output: sihT si a gnirts.
and not: .gnirts a si sihT
Also, how can I do it to a entire file?
Thanks in advance!
Hey guys!
How can I invert (or reverse, whatever) a string, but reversing each word?
Like: This is a string.
Output: sihT si a gnirts.
and not: .gnirts a si sihT
Also, how can I do it to a entire file?
Thanks in advance!
hi here is the complete code with screen shot.. u can easily understand it:
Imports System.IO
Imports System.Text
Public Class Form1
Dim ofd As OpenFileDialog
Dim strbldr As StringBuilder
Dim arr() As String
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'getting the file
ofd = New OpenFileDialog
ofd.ShowDialog()
Me.TextBox1.Text = ofd.FileName
End Sub
'Process
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
strbldr = New StringBuilder
'storing the contents delemeted by space in arr array
arr = strbldr.Append(File.ReadAllText(ofd.FileName).ToString()).ToString().Split(" ")
' reversing each word
For i = 0 To arr.GetUpperBound(0)
arr(i) = ReverseString(arr(i))
Next
strbldr = New StringBuilder
'putting together all the reversed word
For i = 0 To arr.GetUpperBound(0)
strbldr.Append(arr(i))
strbldr.Append(" ")
Next
' writing to original file
File.WriteAllText(ofd.FileName, strbldr.ToString())
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
End Class
i hope it helps
yeH syug!
woH nac I trevni ro( esrever, revetahw) a gnirts, tub gnisrever hcae drow?
ekiL:
sihT si a gnirts.
tuptuO:This is a string.
dna ton:string. a is This
oslA, woh nac I od ti ot a eritne elif?sknahT ni ecnavda!
teL em wonk fi siht spleh.:D
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myFile As String = "C:\test.txt" '// your File.
If IO.File.Exists(myFile) Then '// Check if File.Exists.
MsgBox(reverseFileContent(myFile)) '// Display Result.
IO.File.WriteAllText(myFile, reverseFileContent(myFile)) '// Save Reversed File.
Else : MsgBox("File Does Not Exist.")
End If
End Sub
Function reverseFileContent(ByVal mySelectedFile As String) As String
Dim arFileLines() As String = IO.File.ReadAllLines(mySelectedFile) '// Load each line as a String Array.
Dim arWords() As String = Nothing '// used to Split. each line into Words.
Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter.
Dim sTemp As String = Nothing '// used to save Reversed Lines.
For iFileLineNumber As Integer = 0 To arFileLines.Length - 1 '// Loop thru all Lines.
If arFileLines(iFileLineNumber).Contains(" "c) Then '// Check if Current Line Contains. a "space".
arWords = arFileLines(iFileLineNumber).Split(" "c) '// Split. Line into Words by "space".
For iWord As Integer = 0 To arWords.Length - 1 '// Loop thru all Words.
If Not arWords(iWord) = Nothing Then '// check if Word is not Nothing, since Words get Split. by " ".
If Char.IsLetter(CChar(arWords(iWord).Substring(arWords(iWord).Length - 1, 1))) Then '// check if Last Char is a Letter.
arWords(iWord) = StrReverse(arWords(iWord)) '// Reverse the Word.
Else '// if Last Char is NOT a Letter.
sChar = arWords(iWord).Substring(arWords(iWord).Length - 1, 1) '// get Last Char in Word.
'// Reverse Word without the Last Char, then add Last Char back to Word.
arWords(iWord) = StrReverse(arWords(iWord).Substring(0, arWords(iWord).Length - 1)) & sChar
End If
'// Add Reversed Words to String.
If Not iWord = arWords.Length - 1 Then : sTemp &= arWords(iWord) & " " '// if Not End of Line, add a "space".
Else : sTemp &= arWords(iWord) '// No "space".
End If
Else '// if Word is Nothing, since Words get Split. by " ", add the "space" back to keep File Format for Line Indenting.
sTemp &= " "
End If
Next
Else '// For Lines that DO NOT Contain Spaces, just a Single Word Line.
If Not arFileLines(iFileLineNumber) = Nothing Then '// check if Word is not Nothing, since Words get Split. by " ".
If Char.IsLetter(CChar(arFileLines(iFileLineNumber).Substring(arFileLines(iFileLineNumber).Length - 1, 1))) Then '// check if Last Char is a Letter.
arFileLines(iFileLineNumber) = StrReverse(arFileLines(iFileLineNumber)) '// Reverse the Word.
Else '// if Last Char is NOT a Letter.
sChar = arFileLines(iFileLineNumber).Substring(arFileLines(iFileLineNumber).Length - 1, 1) '// get Last Char in Word.
'// Reverse Word without the Last Char, then add Last Char back to Word.
arFileLines(iFileLineNumber) = StrReverse(arFileLines(iFileLineNumber).Substring(0, arFileLines(iFileLineNumber).Length - 1)) & sChar
End If
sTemp &= arFileLines(iFileLineNumber) '// Add Reversed Word to String.
Else '// if Word is Nothing, since Words get Split. by " ", add the "space" back to keep File Format for Line Indenting.
sTemp &= " "
End If
End If
If Not iFileLineNumber = arFileLines.Length - 1 Then sTemp &= vbNewLine '// Add Line Breaks if Not Last Line.
Next
Return sTemp '// Returns Reversed File Content.
End Function
End Class
hi here is the complete code with screen shot.. u can easily understand it:
Imports System.IO Imports System.Text Public Class Form1 Dim ofd As OpenFileDialog Dim strbldr As StringBuilder Dim arr() As String Dim i As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'getting the file ofd = New OpenFileDialog ofd.ShowDialog() Me.TextBox1.Text = ofd.FileName End Sub 'Process Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click strbldr = New StringBuilder 'storing the contents delemeted by space in arr array arr = strbldr.Append(File.ReadAllText(ofd.FileName).ToString()).ToString().Split(" ") ' reversing each word For i = 0 To arr.GetUpperBound(0) arr(i) = ReverseString(arr(i)) Next strbldr = New StringBuilder 'putting together all the reversed word For i = 0 To arr.GetUpperBound(0) strbldr.Append(arr(i)) strbldr.Append(" ") Next ' writing to original file File.WriteAllText(ofd.FileName, strbldr.ToString()) End Sub ' Reverse Function Public Function ReverseString(ByRef strToReverse As String) As String Dim result As String = "" For i As Integer = 0 To strToReverse.Length - 1 result += strToReverse(strToReverse.Length - 1 - i) Next Return result End Function End Class
i hope it helps
It worked, but it doesn't care about dots. :P
teL em wonk fi siht spleh.:D
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myFile As String = "C:\test.txt" '// your File. If IO.File.Exists(myFile) Then '// Check if File.Exists. MsgBox(reverseFileContent(myFile)) '// Display Result. IO.File.WriteAllText(myFile, reverseFileContent(myFile)) '// Save Reversed File. Else : MsgBox("File Does Not Exist.") End If End Sub Function reverseFileContent(ByVal mySelectedFile As String) As String Dim arFileLines() As String = IO.File.ReadAllLines(mySelectedFile) '// Load each line as a String Array. Dim arWords() As String = Nothing '// used to Split. each line into Words. Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter. Dim sTemp As String = Nothing '// used to save Reversed Lines. For iFileLineNumber As Integer = 0 To arFileLines.Length - 1 '// Loop thru all Lines. If arFileLines(iFileLineNumber).Contains(" "c) Then '// Check if Current Line Contains. a "space". arWords = arFileLines(iFileLineNumber).Split(" "c) '// Split. Line into Words by "space". For iWord As Integer = 0 To arWords.Length - 1 '// Loop thru all Words. If Not arWords(iWord) = Nothing Then '// check if Word is not Nothing, since Words get Split. by " ". If Char.IsLetter(CChar(arWords(iWord).Substring(arWords(iWord).Length - 1, 1))) Then '// check if Last Char is a Letter. arWords(iWord) = StrReverse(arWords(iWord)) '// Reverse the Word. Else '// if Last Char is NOT a Letter. sChar = arWords(iWord).Substring(arWords(iWord).Length - 1, 1) '// get Last Char in Word. '// Reverse Word without the Last Char, then add Last Char back to Word. arWords(iWord) = StrReverse(arWords(iWord).Substring(0, arWords(iWord).Length - 1)) & sChar End If '// Add Reversed Words to String. If Not iWord = arWords.Length - 1 Then : sTemp &= arWords(iWord) & " " '// if Not End of Line, add a "space". Else : sTemp &= arWords(iWord) '// No "space". End If Else '// if Word is Nothing, since Words get Split. by " ", add the "space" back to keep File Format for Line Indenting. sTemp &= " " End If Next Else '// For Lines that DO NOT Contain Spaces, just a Single Word Line. If Not arFileLines(iFileLineNumber) = Nothing Then '// check if Word is not Nothing, since Words get Split. by " ". If Char.IsLetter(CChar(arFileLines(iFileLineNumber).Substring(arFileLines(iFileLineNumber).Length - 1, 1))) Then '// check if Last Char is a Letter. arFileLines(iFileLineNumber) = StrReverse(arFileLines(iFileLineNumber)) '// Reverse the Word. Else '// if Last Char is NOT a Letter. sChar = arFileLines(iFileLineNumber).Substring(arFileLines(iFileLineNumber).Length - 1, 1) '// get Last Char in Word. '// Reverse Word without the Last Char, then add Last Char back to Word. arFileLines(iFileLineNumber) = StrReverse(arFileLines(iFileLineNumber).Substring(0, arFileLines(iFileLineNumber).Length - 1)) & sChar End If sTemp &= arFileLines(iFileLineNumber) '// Add Reversed Word to String. Else '// if Word is Nothing, since Words get Split. by " ", add the "space" back to keep File Format for Line Indenting. sTemp &= " " End If End If If Not iFileLineNumber = arFileLines.Length - 1 Then sTemp &= vbNewLine '// Add Line Breaks if Not Last Line. Next Return sTemp '// Returns Reversed File Content. End Function End Class
Pretty nice code! Do exactly what I want. I just can't figure now on how to do it using textboxes (:
Btw, talking about text boxes... When I use a simple reversion (each string), if I have more than one line, my output is a single line always. How can I bypass it?
Not quite understanding your question.
..If you need to have a TextBox get reversed as the File does, try this quick fix.
Change this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myFile As String = "C:\test.txt" '// your File.
If IO.File.Exists(myFile) Then '// Check if File.Exists.
MsgBox(reverseFileContent(myFile)) '// Display Result.
IO.File.WriteAllText(myFile, reverseFileContent(myFile)) '// Save Reversed File.
Else : MsgBox("File Does Not Exist.")
End If
End Sub
Function reverseFileContent(ByVal mySelectedFile As String) As String
Dim arFileLines() As String = IO.File.ReadAllLines(mySelectedFile) '// Load each line as a String Array.
Dim arWords() As String = Nothing '// used to Split. each line into Words.
Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter.
To:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = reverseFileContent(TextBox1.Lines)
End Sub
Function reverseFileContent(ByVal arFileLines() As String) As String
'// Load each TextBox Line as a String Array.
Dim arWords() As String = Nothing '// used to Split. each line into Words.
Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter.
Not quite understanding your question.
..If you need to have a TextBox get reversed as the File does, try this quick fix.Change this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myFile As String = "C:\test.txt" '// your File. If IO.File.Exists(myFile) Then '// Check if File.Exists. MsgBox(reverseFileContent(myFile)) '// Display Result. IO.File.WriteAllText(myFile, reverseFileContent(myFile)) '// Save Reversed File. Else : MsgBox("File Does Not Exist.") End If End Sub Function reverseFileContent(ByVal mySelectedFile As String) As String Dim arFileLines() As String = IO.File.ReadAllLines(mySelectedFile) '// Load each line as a String Array. Dim arWords() As String = Nothing '// used to Split. each line into Words. Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter.
To:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = reverseFileContent(TextBox1.Lines) End Sub Function reverseFileContent(ByVal arFileLines() As String) As String '// Load each TextBox Line as a String Array. Dim arWords() As String = Nothing '// used to Split. each line into Words. Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter.
I want both. But, the main interface consists of two text boxes, one with the original text and other with the reversed. I'll Add an checkbox that will active the 'word by word' option, thats why I need it to work with textboxes.
I'll try it, thanks (:
It worked perfect! Thanks!
I'm having a new trouble with it. Here is my code:
Public Class JanelaPrincipal
Dim TextoOriginal As String = ""
Dim TextoInvertido As String
Dim TextoTemporario As String
Dim S As New SaveFileDialog
Private Sub InverteTextoOriginal(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boxTextoOriginal.TextChanged
If menuLetraPorLetra.Checked = True Then
TextoOriginal = boxTextoOriginal.Text
TextoInvertido = StrReverse(TextoOriginal)
boxTextoInvertido.Text = TextoInvertido
ElseIf menuPalavraPorPalavra.Checked = True Then
boxTextoInvertido.Text = reverseFileContent(boxTextoOriginal.Lines)
End If
End Sub
Private Sub InverteTextoInvertido(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boxTextoInvertido.TextChanged
If menuLetraPorLetra.Checked = True Then
TextoOriginal = boxTextoInvertido.Text
TextoInvertido = StrReverse(TextoOriginal)
boxTextoOriginal.Text = TextoInvertido
ElseIf menuPalavraPorPalavra.Checked = True Then
boxTextoOriginal.Text = reverseFileContent(boxTextoInvertido.Lines)
End If
End Sub
Private Sub TrocaValores(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInverterValores.Click
TextoTemporario = boxTextoInvertido.Text
boxTextoInvertido.Text = boxTextoOriginal.Text
boxTextoOriginal.Text = TextoTemporario
TextoTemporario = ""
End Sub
Private Sub ExportarTextos(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuExportarTextos.Click
With S
.Filter = "Texto|*.txt"
.FileName = "Texto invertido"
End With
If S.ShowDialog() = DialogResult.OK Then
Dim salvar As New System.IO.StreamWriter(S.FileName)
salvar.Write("Texto original:")
salvar.WriteLine()
salvar.WriteLine()
salvar.Write(boxTextoOriginal.Text)
salvar.WriteLine()
salvar.WriteLine()
salvar.Write("Texto Invertido:")
salvar.WriteLine()
salvar.WriteLine()
salvar.Write(boxTextoInvertido.Text)
salvar.Close()
End If
End Sub
Private Sub JanelaPrincipal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myTrackBar As New TrackBar '// Declare New TrackBar.
With myTrackBar
.Minimum = 8
.Maximum = 72
.TickFrequency = 4
.BackColor = Color.White
End With
AddHandler myTrackBar.Scroll, AddressOf TrackBar_Scroll '// Give it a Event to Handle.
Dim myCtlHost As New ToolStripControlHost(myTrackBar)
OpçõesToolStripMenuItem.DropDownItems.Add(myCtlHost) '// Add to Main Menu.
End Sub
Private Sub TrackBar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Handles TrackBar1.Scroll
Dim trk As TrackBar = CType(sender, TrackBar)
Me.Text = trk.Value.ToString '// For testing.
boxTextoInvertido.Font = New Font("Lucida Sans Unicode", trk.Value) '// Set Font Name and Size.
boxTextoOriginal.Font = New Font("Lucida Sans Unicode", trk.Value) '// Set Font Name and Size.
End Sub
Function reverseFileContent(ByVal arFileLines() As String) As String
'// Load each TextBox Line as a String Array.
Dim arWords() As String = Nothing '// used to Split. each line into Words.
Dim sChar As String = Nothing '// used to get/set last char of word if not a Letter.
Dim sTemp As String = Nothing '// used to save Reversed Lines.
For iFileLineNumber As Integer = 0 To arFileLines.Length - 1 '// Loop thru all Lines.
If arFileLines(iFileLineNumber).Contains(" "c) Then '// Check if Current Line Contains. a "space".
arWords = arFileLines(iFileLineNumber).Split(" "c) '// Split. Line into Words by "space".
For iWord As Integer = 0 To arWords.Length - 1 '// Loop thru all Words.
If Not arWords(iWord) = Nothing Then '// check if Word is not Nothing, since Words get Split. by " ".
If Char.IsLetter(CChar(arWords(iWord).Substring(arWords(iWord).Length - 1, 1))) Then '// check if Last Char is a Letter.
arWords(iWord) = StrReverse(arWords(iWord)) '// Reverse the Word.
Else '// if Last Char is NOT a Letter.
sChar = arWords(iWord).Substring(arWords(iWord).Length - 1, 1) '// get Last Char in Word.
'// Reverse Word without the Last Char, then add Last Char back to Word.
arWords(iWord) = StrReverse(arWords(iWord).Substring(0, arWords(iWord).Length - 1)) & sChar
End If
'// Add Reversed Words to String.
If Not iWord = arWords.Length - 1 Then : sTemp &= arWords(iWord) & " " '// if Not End of Line, add a "space".
Else : sTemp &= arWords(iWord) '// No "space".
End If
Else '// if Word is Nothing, since Words get Split. by " ", add the "space" back to keep File Format for Line Indenting.
sTemp &= " "
End If
Next
Else '// For Lines that DO NOT Contain Spaces, just a Single Word Line.
If Not arFileLines(iFileLineNumber) = Nothing Then '// check if Word is not Nothing, since Words get Split. by " ".
If Char.IsLetter(CChar(arFileLines(iFileLineNumber).Substring(arFileLines(iFileLineNumber).Length - 1, 1))) Then '// check if Last Char is a Letter.
arFileLines(iFileLineNumber) = StrReverse(arFileLines(iFileLineNumber)) '// Reverse the Word.
Else '// if Last Char is NOT a Letter.
sChar = arFileLines(iFileLineNumber).Substring(arFileLines(iFileLineNumber).Length - 1, 1) '// get Last Char in Word.
'// Reverse Word without the Last Char, then add Last Char back to Word.
arFileLines(iFileLineNumber) = StrReverse(arFileLines(iFileLineNumber).Substring(0, arFileLines(iFileLineNumber).Length - 1)) & sChar
End If
sTemp &= arFileLines(iFileLineNumber) '// Add Reversed Word to String.
Else '// if Word is Nothing, since Words get Split. by " ", add the "space" back to keep File Format for Line Indenting.
sTemp &= " "
End If
End If
If Not iFileLineNumber = arFileLines.Length - 1 Then sTemp &= vbNewLine '// Add Line Breaks if Not Last Line.
Next
Return sTemp '// Returns Reversed File Content.
End Function
End Class
Whenever I choose the 'word by word' option, and write a word, as soon as a write a space, my app hangs, and doesn't even give me a error. What I've did wrong?
Nevermind, I found it. I was doing the reversions on both textboxes at the same time.
:D
I'm sure such happens even to the best of us. It's part of being "human".
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.