I have some live data which contains personal information (names/addresses). I want to jumble it so I can take a copy of it as test data without worrying about security.
I want to parse the string and replace characters as follows
Any character in 'a, e, i, o, u' I will replace with another character from the same list
Any character in 'c, g, j, k, q, s, x, z' I will replace with another character from the same list etc.
So 'a' might be replaced with 'e', then 'i' etc
In rough pseudo code terms
foreach (char c in nameStr)
switch(char)
case 'a,e,i,o,u': replace with random from array of a,e,i,o,u
I've not got a huge amount of data to trawl through and I can just set this off and leave it running so speed isn't that much of an issue.
Cheers Martin.
AlanPosted Sep 3, 2008, 5:02 PM
Try this:
using System;
class Program
{
static Random rand = new Random();
static void Main()
{
char[] vowels = new char[]{'a', 'e', 'i', 'o', 'u'};
char[] consonants = new char[]{'c', 'g', 'j', 'k', 'q', 's', 'x', 'z'};
char[] uVowels = new char[]{'A', 'E', 'I', 'O', 'U'};
char[] uConsonants = new char[]{'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z'};
string text = "The Quick Brown Fox Jumps Over The Lazy Dog";
Console.WriteLine(text);
char[] temp = text.ToCharArray();
int index = -1;
for(int i = 0; i < temp.Length; i++)
{
if ((index = Array.IndexOf(vowels, temp[i])) > - 1)
{
temp[i] = GetDifferentRandomChar(index, vowels);
}
else if ((index = Array.IndexOf(consonants, temp[i])) > -1)
{
temp[i] = GetDifferentRandomChar(index, consonants);
}
else if ((index = Array.IndexOf(uVowels, temp[i])) > - 1)
{
temp[i] = GetDifferentRandomChar(index, uVowels);
}
else if ((index = Array.IndexOf(uConsonants, temp[i])) > -1)
{
temp[i] = GetDifferentRandomChar(index, uConsonants);
}
}
string text2 = new string(temp);
Console.WriteLine(text2);
Console.ReadLine();
}
static char GetDifferentRandomChar(int index, char[] chars)
{
int len = chars.Length;
int next;
do
{
next = rand.Next(0, len); // excludes 'len'
}
while (next == index);
return chars[next];
}
}
Martin StephensonPosted Sep 4, 2008, 6:43 AM