function to scramble word

Hello guys,

I need a function that can scramble word randomly.

This is my word = house  

this is my word scrambled = oshue

if I have a sentence, it must scramble it respecting the space among the words, example:

I have a house = I evah a eshou

thanks
thanks
LVL 1
hidrauAsked:
Who is Participating?
 
MerijnBSr. Software EngineerCommented:
place all the characters you want treated as space in the constant DONTSCRAMBLE

usage:
caption := ScrambleSentence('I have a house, it''s on the prairy!');

function ScrambleWord(input: string): string;
var i: integer;
begin
 // it's possible that by pure 'luck' the scrambled word is the same as our input
 // in that case try again

 repeat
  result := '';

  for i := 1 to Length(Input) do
   Insert(input[i], result, Random(Length(result) + 2));
 until (Length(Input) < 2) or (Input <> result);
end;

function ScrambleSentence(input: string): string;
const DONTSCRAMBLE = ' ,.;!?';
var i: integer;
    CurWord: string;
begin
 result := '';
 CurWord := '';

 for i := 1 to Length(Input) do
 begin
  if Pos(Input[i], DONTSCRAMBLE) = 0 then // is this somewhere in a word?
   CurWord := CurWord + Input[i]
  else
  begin                                 // this is a no-scramble character
   result := result + ScrambleWord(CurWord);
   CurWord := '';
   result := result + input[i];
  end;
 end;

 result := result + ScrambleWord(CurWord);
end;
0
 
hidrauAuthor Commented:
thanks very much

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.