Click here to Skip to main content
15,505,601 members
Articles / Multimedia / GDI
Technical Blog
Posted 19 Sep 2014

Tagged as

Stats

25.2K views
4 bookmarked

Sample Code to Scramble a Word using C# String object

Rate me:
Please Sign up or sign in to vote.
2.89/5 (6 votes)
19 Sep 2014CPOL
This post shows sample code to scramble a word using C# string object

Introduction

I was developing a small C# program to generate and print scrambled words puzzle for kids, which are generally called 'Jumbled Words' by many children. I was looking for some C# or VB.NET sample code to scramble the letters in the given word. For example, if the given word is "POLICE", I should be able to randomly scramble the letters in the word and generate a new word something like this: "COLPIE" or "ICELPO". I found a couple of examples in various sites, but I wasn't convinved with most of them. So I decided to write a sample myself. Here is the code I came up with to scramble the given word to develop my Jumbled Words puzzle:

C#
 public string ScrambleWord(string word) 
{ 
    char[] chars = new char[word.Length]; 
    Random rand = new Random(10000); 
    int index = 0; 
    while (word.Length > 0) 
    { // Get a random number between 0 and the length of the word. 
        int next = rand.Next(0, word.Length - 1); // Take the character from the random position 
                                                  //and add to our char array. 
        chars[index] = word[next];                // Remove the character from the word. 
        word = word.Substring(0, next) + word.Substring(next + 1); 
        ++index; 
    } 
    return new String(chars); 
}  

How to Call the string Scramble Method?

It is very easy to use the above method to scramble the words. See an example below:

C#
string word = "POLICE"; string scrambled_Word = ScrambleWord(word); 

This C# sample is short and efficient, compared to some other pretty big programs I could find on the web to scramble strings. There is nothing specific to C# here, so you can easily change the syntax a bit to make your own VB.NET word scramble program.

(This article was originally published here).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Tony is a seasoned .NET developer who recently switched his focus to Windows 8 and SEO. You may visit his technology websites: www.dotnetspider.com and www.techulator.com.

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult19-Sep-14 5:19
professionalPIEBALDconsult19-Sep-14 5:19 
GeneralMy vote of 1 Pin
Prerak Patel19-Sep-14 5:08
professionalPrerak Patel19-Sep-14 5:08 
GeneralRe: My vote of 1 Pin
Thanks787219-Sep-14 8:07
professionalThanks787219-Sep-14 8:07 
SuggestionLINQ does this in just one line Pin
Prerak Patel19-Sep-14 5:07
professionalPrerak Patel19-Sep-14 5:07 
QuestionHi Pin
alejandro29A19-Sep-14 4:30
alejandro29A19-Sep-14 4:30 
I think you have implemented the Fisher-Yates Shuffle[^]...
Dios existe pero duerme...
Sus pesadillas son nuestra existencia.
(Ernesto Sabato)

QuestionWhy not as an extension method ? Pin
Catalin Hatmanu19-Sep-14 4:15
Catalin Hatmanu19-Sep-14 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.