?
Solved

shuffle a string?

Posted on 2003-02-27
16
Medium Priority
?
854 Views
Last Modified: 2012-05-04
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
Comment
Question by:boofulls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 7

Expert Comment

by:lavinder
ID: 8032667
0
 

Author Comment

by:boofulls
ID: 8032702
Microsoft VBScript compilation error '800a03ea'

Syntax error

/1/template.asp, line 112

Function Shuffle(txt As String) As String

0
 
LVL 7

Expert Comment

by:lavinder
ID: 8032840
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:boofulls
ID: 8032887
did that but ...


Microsoft VBScript compilation error '800a03ea'

Syntax error

/1/template.asp, line 113

Function Shuffle(txt)



0
 

Author Comment

by:boofulls
ID: 8034281
can anyone help?
0
 

Author Comment

by:boofulls
ID: 8034495
can anyone help?
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8039320
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
 
LVL 15

Expert Comment

by:gladxml
ID: 8039348
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
 

Author Comment

by:boofulls
ID: 8040727
same deal

Microsoft VBScript compilation error '800a03ea'

Syntax error

/1/template.asp, line 120

Function ReOrderArrayQuickNDirty(ByVal aArray)

0
 
LVL 15

Accepted Solution

by:
gladxml earned 440 total points
ID: 8045548
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
 
LVL 15

Expert Comment

by:gladxml
ID: 8045640
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
 
LVL 15

Expert Comment

by:gladxml
ID: 8207447
boofulls,

What is the status of this question?

Regards,
gladxml
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8448554
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
 

Expert Comment

by:CleanupPing
ID: 9576830
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
 
LVL 58

Expert Comment

by:Gary
ID: 9753756
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question