~Matt~ Posted February 6, 2009 Share Posted February 6, 2009 I was wondering if someone could tell/show me how to scramble the text between the first and last letter of each word. eg. the word "scramble" would become something like "srcabmle". Link to comment Share on other sites More sharing options...
0 James Rose Posted February 6, 2009 Share Posted February 6, 2009 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) Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 6, 2009 Veteran Share Posted February 6, 2009 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: Break down the sentence into its component words. For each word store its internal component characters (between the first and last letter). Swap the letters of these internal components. 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 Find the project attached. Hope that helps :) WordScrambler___VB2008.zip Link to comment Share on other sites More sharing options...
0 ~Matt~ Posted February 6, 2009 Author Share Posted February 6, 2009 (edited) Thanks =] Yer that achieved what I wanted. Edited February 6, 2009 by ~Matt~ Link to comment Share on other sites More sharing options...
Question
~Matt~
I was wondering if someone could tell/show me how to scramble the text between the first and last letter of each word. eg. the word "scramble" would become something like "srcabmle".
Link to comment
Share on other sites
3 answers to this question
Recommended Posts