• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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
0
hidrau
Asked:
hidrau
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now