1

I'm looking to replace a string with random values - and keep the same length. However, I'd like all characters to be replaced with chars, digits to be replaced with digits.

I'm wondering the best way to do this. I'm considering a for loop through each character but that could be potentially quite performance intensive.

I may be wrong, in which case please do let me know.

Thanks

user270370
  • 151
  • 1
  • 3
  • 8
  • This does sound like homework. What's your business requirement for this? If its encryption or something, there are better ways. – schummbo May 26 '11 at 16:41
  • @schummbo, if it is random then the encryption will be pretty good, not many better ways to encrypt than to completely destroy the original with random spew :) – Chris Taylor May 26 '11 at 17:07
  • @Chris I guess I should have said, "More USEABLE ways" :) – schummbo May 26 '11 at 17:08

6 Answers6

2

Unless you've got a performance requirement and/or problem, don't micro-optimize. Just use a loop.

Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
2

You are wrong. To know whether it is a character or a digit, you need to look at each value in the string, so you need to loop over the string in any case.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

How else are you going to do it without looping thorough each character? At a minimum, you need to look to see if the character is a digit or not and replace it. I'll assume you can make a function called RandomChar and RandomDigit. And this will be written more c++ ish than c# ish, but you get the idea:

for (int i=0;i<myStr.Length();++i)
{
  c=myStr[i];
  if(isDigit(c)) 
  {
    c=RandomDigit();
  }
  else
  {
    c=RandomChar();
  }
  myStr[i]=c;
}

There's really no other way since you need to inspect each character anyway.

the functions isDigit, RandomDigit, and RandomChar are left as exercises to the reader.

miked
  • 3,458
  • 1
  • 22
  • 25
1

If it is a long string it can be since changes to a string cause a new object to be created. I would use a for loop but convert your string to a char array manipulate and then back to a string.

rerun
  • 25,014
  • 6
  • 48
  • 78
  • -1 while technically true, I seriously doubt this programmer should be worried about performance when doing something like this. It sounds like he doesn't understand how fast computers are. – Byron Whitlock May 26 '11 at 16:37
  • So a technicaly true answer is a -1. sok. If you have a very long string and manipulate it in this way and its not a test project you can easily get yourself in trouble. Most dev don't understand what immutability implies and write long string manipulatory functions that are serious performance issues. Also since my suggestion takes an additional 2 lines of code I would not consider it onerous to alleviate the potential issue. – rerun May 26 '11 at 16:43
  • so regardless of string size looping would be fine? – user270370 May 26 '11 at 16:58
  • fair enough, though a StringBuilder.Append() would work as well. – Byron Whitlock May 26 '11 at 17:04
  • @user - It would be fine if this is only homework. If you plan on using this to actually solve a actual problem you have its the wrong way to go about it. – Security Hound May 26 '11 at 17:06
0

(I have assumed you already have methods for generating random characters.)

var source = "RUOKICU4T";
var builder = new StringBuilder(source.Length);

for (int index = 0; index < builder.Length; index += 1)
{
    if (Char.IsDigit(source[index]))
    {
        builder[index] = GetRandomDigit();
    }
    else if (Char.IsLetter(source[index]))
    {
        builder[index] = GetRandomLetter();
    }
}

string result = builder.ToString();
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
0

Consider using LINQ to help avoid explict loops. You can refactor to ensure that numbers

static void Main()
{
    string value = "She sells 2008 sea shells by the (foozball)";

    string foo = string.Join("", value
                                .ToList()
                                .Select(x => GetRand(x))
                                );
    Console.WriteLine(foo);
    Console.Read();
}


private static string GetRand(char x)
{             
    int asc = Convert.ToInt16(x);            
    if (asc >= 48 && asc <= 57)
    {
        //get a digit
        return  (Convert.ToInt16(Path.GetRandomFileName()[0]) % 10).ToString();       
    }
    else if ((asc >= 65 && asc <= 90)
          || (asc >= 97 && asc <= 122))
    {
        //get a char
        return Path.GetRandomFileName().FirstOrDefault(n => Convert.ToInt16(n) >= 65).ToString();
    }
    else
    { return x.ToString(); }
}
p.campbell
  • 98,673
  • 67
  • 256
  • 322