Jump to content

Pascal!? I have some string with text, how to shuffle the words in it?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Stasonix

Stasonix

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal
for example:

instring:='one two three, common? let's go! anything';

outstring:='two common let's anything go three one';


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You'll first need to split the words in the string into an array of strings, then select them in a random order.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Pascal being an old language with lesser people now actively using and knowing it's syntax, i thought a few links of sample codes would be helpful.

String manipulation functions in Pascal using Borland's turbo pascal compiler.
TURBO PASCAL STRINGS

As mentioned by Winged Panther, create an array of strings using syntax here if you have the above compiler.

Some body wrote a reverse string code in pascal that might help in becoming more familiar with syntax.
String Manipulation - Pascal - Forums at ProgrammersHeaven.com

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The other relevant question: is this Turbo Pascal, ObjectPascal, FreePascal, other?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Actor

Actor

    Newbie

  • Members
  • PipPip
  • 24 posts

fayyazlodhi said:

Pascal being an old language with lesser people now actively using and knowing it's syntax, ...
The words "fewer people", rather than "lesser people" would be more considerate of those of us who still use the language.

#6
Grim

Grim

    Newbie

  • Members
  • Pip
  • 1 posts
This is my 1st post so, hello to everyone!
It's like WingedPanther said, but i'd use TStringList instead of arrays:


function GetToken(Text, SeperateChar: string; Position: Byte): string;

var

  Token: string;

  Num, Len: Integer;

begin

  Num := 1;

  Len := Length(Text);

  while (Num <= Position) and (Len <> 0) do begin

    Len := Pos(SeperateChar, Text);

    if (Len <> 0) then begin

      Token := Copy(Text, 1, Len - 1);

      Delete(Text, 1, Len);

      Inc(Num);

    end else Token := Text;

  end;

  Result := Token;

end;


function RandomizeStr(Text, SeperateChar: string): string;

var

  outString: string;

  i, TokenRand, TokenCount: integer;

  TokenList: TStringList;

begin

  TokenCount := Length(Text);

  TokenCount := TokenCount - Length(StringReplace(Text, SeperateChar, '', [rfReplaceAll, rfIgnoreCase]));

  Inc(TokenCount);

  TokenList := TStringList.Create;

  for i := 1 to TokenCount do TokenList.Add(GetToken(Text, SeperateChar, i));

  Randomize;

  for i := 1 to TokenCount do begin

    TokenRand := Random(TokenList.Count);

    outString := outString + SeperateChar + TokenList.Strings[TokenRand];

    TokenList.Delete(TokenRand);

  end;

  Result := trim(outString);

end;


// inString := 'one two three, common? let''s go! anything';

// outString := RandomizeStr(inString, ' ');


Yep i wrote that in Delphi. Hope there are not many differences.

Edited by Grim, 23 August 2011 - 07:22 AM.
typos ;(





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users