• 0

[VB.NET] Scrambling text between the first and last letter


Question

3 answers to this question

Recommended Posts

  • 0

Take a look at StrReverse http://msdn.microsoft.com/en-us/library/e462ax87(VS.71).aspx You can get the length of the string

Dim sValue as String = "Scramble"

Dim iLength as Int16 = sValue.Length

And then decided how many characters you want to reverse. Multiple runs through StrReverse will get you want you want. (Though I cant see the value of this. Please please please tell me this is not a password issue)

  • 0

Are you trying to produce something like this:

"Plesae use the floilonwg foramt wehn saintrtg a new toipc taht reaetls to a sinlge ponmgamirrg lnagague"

I've put together a VB2008 Project for you to demonstate how this can be achieved. The general premise is:

  1. Break down the sentence into its component words.
  2. For each word store its internal component characters (between the first and last letter).
  3. Swap the letters of these internal components.
  4. Re-write the word, preserving the first and last letters, with the swapped letters internally.

You need to utilise a few char arrays, and then a simple scrambling function:

Private Function ScrambleWord(ByVal word As String) As String
	Dim i As Integer = 0
	Dim builder As System.Text.StringBuilder = New System.Text.StringBuilder()
	Dim random As Random = New Random()
	Dim index As Integer = 0

	Dim lower As Integer = 0
	Dim upper As Integer = 0

	Dim parts() As Char
	Dim part As Char

	If Not (String.IsNullOrEmpty(word)) Then
		If (word.Length > 3) Then
			parts = word.ToCharArray()
			builder.Append(word.Substring(0, 1))
			parts = word.Substring(1, word.Length - 2).ToCharArray()

			lower = LBound(parts) : upper = UBound(parts)
			For i = lower To upper
				index = random.Next(lower, upper)
				part = parts(index)
				parts(index) = parts(i)
				parts(i) = part
			Next
			builder.Append(parts)
			builder.Append(word.Substring(word.Length - 1, 1))
			Return builder.ToString()
		Else
			Return word
		End If
	Else
		Return String.Empty
	End If
End Function

post-92970-1233934476.jpg

Find the project attached. Hope that helps :)

WordScrambler___VB2008.zipFetching info...

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.