I can achieve reversing each word in a string and keep the order as well, but I believe it can be done quicker using recursion. At the moment I'm going through each character and flipping it, its not the best as it can be slow when the string is long. I'm working on an encryption algorithm if you want to know...
Example: 473hTml je31%@31#
This would result to: lmTh374 #13@%13ej
Any guidance or a good read would be great.
Don't use recursion: it won't be faster. Reversing a string is not a problem that requires recursion.
Actually, VB.Net has a StrReverse
function, so I would advise you to just use that (combine with String.Split
to break your string into words, and String.Join
to put them back together).
Also, don't roll your own crypto - at least, not for protecting anything serious.
I'm no vb expert, but something like this should be a good start.
public string reverseWord(string data) {
if (data.length == 1) {
return data;
} else {
return data.Concat(data.Substring(-1), reverseWord(data.Substring(1));
}
}
Behold, the recursive BogoFlip!
Module Module1
Private rnd As New Random
Dim str As String = "testing"
Sub Main()
Console.WriteLine(str & " " & BogoFlip(str))
Console.ReadLine()
End Sub
Public Function BogoFlip(originalString As String) As String
Dim copy As String = originalString
Dim flipped As String = ""
Do Until copy.Length = 0
Dim index As Integer = rnd.Next(copy.Length)
flipped &= copy(index)
copy = copy.Remove(index, 1)
Loop
If flipped = StrReverse(originalString) Then
Return flipped
Else
Return BogoFlip(originalString)
End If
End Function
End Module
Please don't do this in real life... please please please please.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.