How to Shuffle Characters from a String in Delphi

Asked

Viewed 208 times

3

Would it have any function to shuffle in Delphi to shuffle a string.

  • String Ex := 'wooden house';
  • Return ex := 'medr ci saedaaa';
  • 2

    Give a look if it helps https://www.clubedohardware.com.br/forums/topic/567952-scramblerletters/

  • 1

    That’s right. To not go unanswered can I put the code with the answer? or you put... will the site go off the air...

  • 1

    If you’ve solved it you can put the code to help people.

1 answer

3


Posting not to go unanswered as suggested by the author of the question.

Source: Jefferson Rudolf (users/34982)

function EmbaralharString(const aString: string): string;
var
  i: integer;
  vPosicao: integer;
  vLista: TStrings;
begin
  Randomize;

  vLista := TStringList.Create;

  for i := 1 to aString.Length do
    vLista.Add(aString[i]);

  for i := 1 to aString.Length do
  begin
    vPosicao := Random(vLista.Count);
    Result := Result + vLista.Strings[vPosicao];
    vLista.Delete(vPosicao);
  end;

  FreeAndNil(vLista);
end;

Very simple, first it unmounts the string in and creates a list of "char", then randomly takes each "char" and mounts a new string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.