• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 865
  • Last Modified:

shuffle a string?

hi if my string is

myString="this is a test"

is there anyway to randomly shuffle the string
so it becomes

myString="hist si s etts"

ie preserving where the whitespace is

thanks
0
boofulls
Asked:
boofulls
  • 6
  • 5
  • 2
  • +2
1 Solution
 
lavinderCommented:
0
 
boofullsAuthor Commented:
Microsoft VBScript compilation error '800a03ea'

Syntax error

/1/template.asp, line 112

Function Shuffle(txt As String) As String

0
 
lavinderCommented:
Hi

 This is vbscript, therefore no datatypes, only variants, use it like this

Function Shuffle(txt)
   Dim i,l,j,tmp
   Randomize
   l = Len(txt)
   'Repeat until all the characters in txt are replaced with chr(0)
   Do While txt <> String(l, Chr(0))
      'Generate a random character position within the text string
      i = Fix(Rnd * l) + 1
      'Check to see if it has already been replaced, if not, then replace it with chr(0)
      If Mid$(txt, i, 1) <> Chr(0) Then
         'Add the character at i to the shuffled text string "Shuffle"
         Shuffle = Shuffle & Mid$(txt, i, 1)
         'Replace the character at i with chr(0) (there may be a more "elegant" solution
         'but this works)
         tmp = ""
         For j = 1 To l
            If j = i Then
               tmp = tmp & Chr(0)
            Else
               tmp = tmp & Mid$(txt, j, 1)
            End If
         Next
         txt = tmp
      End If
   Loop
End Function
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
boofullsAuthor Commented:
did that but ...


Microsoft VBScript compilation error '800a03ea'

Syntax error

/1/template.asp, line 113

Function Shuffle(txt)



0
 
boofullsAuthor Commented:
can anyone help?
0
 
boofullsAuthor Commented:
can anyone help?
0
 
gladxmlCommented:
boofulls,

Try to check out this code...

just cut and paste
=======
<%
'****************************************
' This function randomly reorders the
' array aArray using a quick and dirty
' approach...  It's fast!
'****************************************
Function ReOrderArrayQuickNDirty(ByVal aArray)

  Dim iUpper, iLower, iLoop, iSwapPos, varTmp
  iUpper = UBound(aArray)
  iLower = LBound(aArray)

  Randomize Timer

  'Loop through the array, randomly swapping values
  For iLoop = iLower to iUpper
    'Get an array index to swap
    iSwapPos = Int(Rnd * (iUpper + 1))

    'Swap the current element with the element at iSwapPos
    varTmp = aArray(iLoop)
    aArray(iLoop) = aArray(iSwapPos)
    aArray(iSwapPos) = varTmp
  Next

  ReOrderArrayQuickNDirty = aArray   'Return the jumbled array
End Function


'testing string...

myString="this is a test"

arrayMystring = Split(MyString)

limit = UBound(arrayMystring)

Redim sizearray(limit)

'get the size od each word

for x = LBound(arrayMystring) to UBound(arrayMystring)

stringtoshuffle = stringtoshuffle & arrayMystring(x)
sizearray(x) = Len(arrayMystring(x))

next

'put each character in an array

Dim arraytemp
Redim arraytemp(Len(stringtoshuffle) - 1)

For i = 0 to Len(stringtoshuffle) - 1

arraytemp(i) = Mid(stringtoshuffle,i+1,1)

next

Dim iLoop
'Jumble up the array and display the new, random order!
arraytemp = ReOrderArrayQuickNDirty(arraytemp)
For iLoop = LBound(arraytemp) to UBound(arraytemp)
  stringshuffled = stringshuffled & arraytemp(iLoop)
Next

count = 1
Mystring = ""
For isize = LBound(sizearray) to UBound(sizearray)
'arrayMystring(isize) =  Mid(stringshuffled,count,sizearray(isize))
Mystring = Mystring & " " & Mid(stringshuffled,count,sizearray(isize))
count = count + cint(sizearray(isize))

next

response.write Mystring
%>

HTH...

HAppy programming...

0
 
gladxmlCommented:
boofulls,

I have some free time so I made some adjusment to the code base on your requirement...

BTW below is the reference that I had used in solving your problem...

http://www.4guysfromrolla.com/webtech/110800-1.shtml

Regards,
glasdxml

0
 
boofullsAuthor Commented:
same deal

Microsoft VBScript compilation error '800a03ea'

Syntax error

/1/template.asp, line 120

Function ReOrderArrayQuickNDirty(ByVal aArray)

0
 
gladxmlCommented:
boofulls,


 I already tested the code here before I post it and it is working fine and found no such error...

May I ask how do you use the script...

Basically the script will run by itself if you will just cut and paste... save the code as anyfilename.asp

======
As you can see this is the sample string

myString="this is a test"
========

Using the refresh function of the browser you will see that the string is randomly shuffled...


Could you please post the whole source code of this file...

template.asp


HTH...

HAppy programming...

0
 
gladxmlCommented:
boofulls,

Here is the code with input and submit the input to itself and check the result...

just cut and paste
====save thai as shuffle.asp
<%
if request.form("Submit") = "Shuffle" Then

'****************************************
' This function randomly reorders the
' array aArray using a quick and dirty
' approach...  It's fast!
'****************************************
Function ReOrderArrayQuickNDirty(ByVal aArray)

  Dim iUpper, iLower, iLoop, iSwapPos, varTmp
  iUpper = UBound(aArray)
  iLower = LBound(aArray)

  Randomize Timer

  'Loop through the array, randomly swapping values
  For iLoop = iLower to iUpper
    'Get an array index to swap
    iSwapPos = Int(Rnd * (iUpper + 1))

    'Swap the current element with the element at iSwapPos
    varTmp = aArray(iLoop)
    aArray(iLoop) = aArray(iSwapPos)
    aArray(iSwapPos) = varTmp
  Next

  ReOrderArrayQuickNDirty = aArray   'Return the jumbled array
End Function


'testing string...

myString = request.form("texttoshuffle")

arrayMystring = Split(MyString)

limit = UBound(arrayMystring)

Redim sizearray(limit)

'get the size od each word

for x = LBound(arrayMystring) to UBound(arrayMystring)

stringtoshuffle = stringtoshuffle & arrayMystring(x)
sizearray(x) = Len(arrayMystring(x))

next

'put each character in an array

Dim arraytemp
Redim arraytemp(Len(stringtoshuffle) - 1)

For i = 0 to Len(stringtoshuffle) - 1

arraytemp(i) = Mid(stringtoshuffle,i+1,1)

next

Dim iLoop
'Jumble up the array and display the new, random order!
arraytemp = ReOrderArrayQuickNDirty(arraytemp)
For iLoop = LBound(arraytemp) to UBound(arraytemp)
  stringshuffled = stringshuffled & arraytemp(iLoop)
Next

count = 1
Mystring = ""
For isize = LBound(sizearray) to UBound(sizearray)
'arrayMystring(isize) =  Mid(stringshuffled,count,sizearray(isize))
Mystring = Mystring & " " & Mid(stringshuffled,count,sizearray(isize))
count = count + cint(sizearray(isize))

next

Result = "Result: <br>" & Mystring

end if
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="shuffle.asp">

Type a word/sentence to shuffle:<br>
  <textarea name="texttoshuffle" rows="4" cols="30"><%=request.form("texttoshuffle")%></textarea>
<input type="submit" name="Submit" value="Shuffle">
<br>
<br>
<%=Result%>


</form>
</body>
</html>
==========

if you want to cahnge the filename make sure that the action on the form tag is also change with the same filename...

HTH...

HAppy programming....


0
 
gladxmlCommented:
boofulls,

What is the status of this question?

Regards,
gladxml
0
 
gladxmlCommented:
boofulls,

What is the status of this question.

Were any of these comments helpful?

If so, please select one as an answer to close out this question.

In case of a duplicate comment please select the first comment as an answer.

Regards,
gladxml
0
 
CleanupPingCommented:
boofulls:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
GaryCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by gladxml

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now