function to scramble word

Posted on 2007-07-24
Last Modified: 2010-04-04
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

Question by:hidrau
    LVL 19

    Accepted Solution

    place all the characters you want treated as space in the constant DONTSCRAMBLE

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

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

      result := '';

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

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

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

     result := result + ScrambleWord(CurWord);
    LVL 1

    Author Comment

    thanks very much


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now