Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Algorithmen (https://www.delphipraxis.net/28-library-algorithmen/)
-   -   Delphi Permutation/Anagram eines Strings erzeugen (https://www.delphipraxis.net/73770-permutation-anagram-eines-strings-erzeugen.html)

3_of_8 23. Jul 2006 20:06


Permutation/Anagram eines Strings erzeugen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Die folgende Funktion erzeugt ein Anagramm aus einem übergebenen String.

Es ist darauf zu achten, dass vor ihrem Aufruf einmal Randomize; aufgerufen wurde.

Delphi-Quellcode:
function MakeAnagram(str: string): string;
var I, n, r: Integer;
    c: Char;
begin
  result:=str;
  n:=length(result);
  for I:=1 to n do
  begin
    r:=i+random(n-I+1);
    c:=result[I];
    result[I]:=result[r];
    result[r]:=c;
  end;
end;
(Fisher und Yates-Algorithmus)

[edit=Chakotay1308]Titel geändert. Mfg, Chakotay1308[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:02 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz