Shruffle

Started by bplus, November 03, 2019, 15:38:54

Previous topic - Next topic

bplus

An updated version for the old line numbered scramble:

' shruffle.bas SmallBASIC 0.12.13 (B+=MGA) 2019-11-03
randomize timer
while 1
  cls
  input "Enter a word or phrase to shruffle ";shruffleMe
  shruffleMe = str(shruffleMe) 'make sure we are working with a string
  if shruffleMe <> "" then shruffle shruffleMe :  ? shruffleMe else end
  pause 3
wend

sub shruffle(byref s) 'shuffle letters in string s
  for i = len(s) to 2 step -1 'using F-Y algo for shuffle
    r = int(i * rnd) + 1 : si = mid(s, i, 1)
    mmid s, i, 1, mid(s, r, 1)
    mmid s, r, 1, si
  next
end

sub mmid (byref source, position, length, new)
    s = mid(source, 1, position - 1) + mid(new, 1, length) + mid(source, position + length)
end sub

1 person likes this