473,378 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Randomise character positions in string

Does anybody have a simple method for randomising the character
positions in a string?

*** Sent via Developersdex http://www.developersdex.com ***
Feb 25 '08 #1
4 2393
Like a scramble method where "Hello, World!" becomes something like
"leol,H!o rlWd" ?

--
Roger Frost
"Logic Is Syntax Independent"

"Mike P" <mi*******@gmail.comwrote in message
news:eH**************@TK2MSFTNGP05.phx.gbl...
Does anybody have a simple method for randomising the character
positions in a string?

*** Sent via Developersdex http://www.developersdex.com ***
Feb 25 '08 #2
How about (off the top of my head):

public static string Scramble(string input) {
if(string.IsNullOrEmpty(input)) return input; // GIGO

List<charinputChars = new List<char>(input);
char[] outputChars = new char[inputChars.Count];
Random rand = new Random();
// fill backwards to simplify Rand.Next() logic
for (int i = inputChars.Count - 1; i >= 0; i--)
{
int index = rand.Next(i);
outputChars[i] = inputChars[index];
inputChars.RemoveAt(index);
}
return new string(outputChars);
}

public static void Main()
{
string foo = "The quick brown fox jumps over the lazy
dog";
string bar = Scramble(foo);
}
Feb 25 '08 #3
Oh, and at some point will you need to restore the string to it's original
state?

--
Roger Frost
"Logic Is Syntax Independent"
Like a scramble method where "Hello, World!" becomes something like
"leol,H!o rlWd" ?

Feb 25 '08 #4
Roger,

Yes, exactly like that, but I won't need to restore the string to its
original state.
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Feb 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gsb | last post by:
I would like to randomly reorder the characters in a string. I wrote a function breaking the string into arrays, assigning Rangoon numbers and reassembling the string. Is there an easier way? ...
9
by: Franz Steinhaeusler | last post by:
given a string: st="abcdatraataza" ^ ^ ^ ^ (these should be found) I want to get the positions of all single 'a' characters. (Without another 'a' neighbour) So I tried:...
9
by: Safalra | last post by:
The idea here is relatively simple: a java program (I'm using JDK1.4 if that makes a difference) that loads an HTML file, removes invalid characters (or replaces them in the case of common ones...
5
by: jen_designs | last post by:
Is there a way to return the character position on a page? Not the x and y coordinates, but the number of characters on a page. For instance i have a html page with the following text: This is my...
1
by: Colin Green | last post by:
OK here's is what I wish to do. I have an XML file that I want to read into an XmlDocument. I then want to be able to interrogate the XmlNodes to find both their start AND end character positions...
9
by: Christian Kandeler | last post by:
Hi, if I want to store the string "123456" in a variable of type char, I can do it like this: char s = "123456"; Or like this: char s = { '1', '2', '3', '4', '5', '6', '\0' };
0
by: Reddy | last post by:
Please somebody help me to write regular expression for "Same character must not occure in three consecutive positions" And "Password must differ from userid by atleast three positions" Thank...
2
by: Chiefy | last post by:
Hey, Im doing a project in C# and I need to be able to take an array (say StudentNames), randomise the contents, and then place them in the random order into a new array (randomStudentNames). ...
4
by: jameskuyper | last post by:
mkeles84 wrote: The other responses you've received have explained why 'name' needs to contain a Null-Terminated Character String (NTCS). However, it occurred to me that you might not be aware...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.