mapNext(Code, [], []). mapNext(Code, [X|L], [Y|LRes]) :- conseq(Code, X, Y), mapNext(Code, L, LRes). ?- string_codes(' 1U40ckjGEpdeil5xyde9STilbf237wzCKJoOdeilNILstnPMmQXYahVRgDAHuvrBFWZ1 ', Code), string_codes('123', L), mapNext(Code, L, X), write(X). Prolog