# How to scramble a string of text?

How to implement foo function which works like this:

foo @ "abcdef"

"fdbace"


Steps: "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"

Reverse the first two letter of the string "abcdef" which will give "bacdef". Then take this as the new string and reverse the first 3 letter. Similarly, proceed for all the characters of the string.

s = "abcdef";

FoldList[
StringJoin[
StringReverse[StringTake[#1, {1, #2}]],
StringTake[#1, {#2 + 1, -1}]
] &,
s,
Range[2, StringLength[s]]
]


{"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"}

• I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications. – b3m2a1 Oct 1 '18 at 0:02
• @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow... – Henrik Schumacher Oct 1 '18 at 0:11
• I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, {#2 + 1 ;;}] which is again very surprising. The specially made operation should be the fastest one, to my eyes. – b3m2a1 Oct 1 '18 at 0:15
• It turns out String is really fast now – b3m2a1 Oct 1 '18 at 0:56
FoldList[StringReplace[#,  StartOfString ~~ p : Repeated[_, {#2}] :>
StringReverse[p]] &, "abcdef" , Range[2, 6]]


{"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"}

Also

ClearAll[f1, f2]
f1[k_] := Module[{c = TakeDrop[Characters[#], k]},
StringJoin[c[[1]][[-1 ;; 1 ;; -2]], c[[1]][[1 + Boole[OddQ[k]] ;; ;; 2]], c[[2]]] ] &
f2 = Table[f1[k]@# , {k, StringLength @ #}] &;

f2 @ "abcdef"


{"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"}

Another way:

Block[{k = 1, s = "abcdef"},
NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], {1, k}] &,
s, StringLength[s] - 1]]
{"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"}