Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get continuous characters in C#?

I've a

List<String> MyList=new List<string>();

I need to fill the list MyList with n values.

if the value of n is 2 then the list MyList will contain

"A","B"

if 10 then

"A","B","C"....."J"

if 30 then

"A"....."Z","AA","AB",AC","AD"

if 1000 then

"A",....."Z","AA","AB"......"AZ","BA","BB"......."BZ"........"YZ","AAA",AAB".....
and so on

I do not know how to do this.

Please help me to do this using any method Using LINQ or LAMBDA Expression

like image 764
Thorin Oakenshield Avatar asked Nov 02 '10 07:11

Thorin Oakenshield


People also ask

How do you store strings in C?

String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'. Do not confuse the null byte, '\0', with the character '0', the integer 0, the double 0.0, or the pointer NULL.

Is there a string data type in C?

There is no string type in C . You have to use char arrays. By the way your code will not work ,because the size of the array should allow for the whole array to fit in plus one additional zero terminating character.

Where are characters stored in C?

Software Engineering C However, the char type is integer type because underneath C stores integer numbers instead of characters.In C, char values are stored in 1 byte in memory,and value range from -128 to 127 or 0 to 255.


3 Answers

Edit 2:

This is probably the easiest way to implement it. I tested it, it works fine. You could generate a infinite number of strings.

public IEnumerable<string> GenerateStrings()
{
    foreach(string character in Alphabet())
    {
      yield return character;
    }

    foreach (string prefix in GenerateStrings())
    {
      foreach(string suffix in Alphabet())
      {
        yield return prefix + suffix;
      }
    }
}

public IEnumerable<string> Alphabet()
{
    for(int i = 0; i < 26; i++)
    {
      yield return ((char)('A' + i)).ToString();
    }
}

Stuff I wrote before:

You could also write a little recursive function which returns any string by a certain index. This may not be optimal performance wise, because there are some repetitive divisions, but it may be fast enough for your purpose.

It is quite short and easy:

string GetString(int index)
{
  if (index < 26)
  {
    return ((char)('A' + index)).ToString();
  }
  return GetString(index / 26 - 1) + GetString(index % 26);
}

usage (may also be put into another method:

List<string> strings = Enumerable.Range(0, 1000)
  .Select(x => GetString(x))
  .ToList();

This is working code, just wrote a test for it.


Edit: eg, the "full linq way" application of GetString:

public void IEnumerale<string> GenerateStrings()
{
  int index = 0;
  // generate "infinit" number of values ...
  while (true)
  {
     // ignoring index == int.MaxValue
     yield return GetString(index++);
  }
}

List<string> strings = GenerateStrings().Take(1000).ToList();
like image 192
Stefan Steinegger Avatar answered Sep 22 '22 06:09

Stefan Steinegger


I did something similar in SQL a while back.

Translated to C# this is a function to create a code from a number:

public static string GetCode(int id) {
  string code, chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  if (id <= chars.Length) {
    code = chars.Substring(id - 1, 1);
  } else {
    id--;
    int value = chars.Length, adder = 0;
    while (id >= value * (chars.Length + 1) + adder) {
      adder += value;
      value *= chars.Length;
    }
    code = chars.Substring((id - adder) / value - 1, 1);
    id = ((id - adder) % value);
    while (value > 1) {
      value /= chars.Length;
      code += chars.Substring(id / value, 1);
      id = id % value;
    }
  }
  return code;
}

Then you just get numbers from 1 and up, and translate into codes:

var codes = Enumerable.Range(1, 1000).Select(n => GetCode(n));

The limit of the function is currently "ZZZZZZ" or 321272406. (After that you get a division by zero.)

Note that this function uses all combinations and returns "A".."Z", "AA".."ZZ", "AAA"..."ZZZ" rather than starting at "AB" and "ABC".

like image 22
Guffa Avatar answered Sep 20 '22 06:09

Guffa


This is similar to this question (but not quite enough to mark it as a duplicate, and it's a hard problem to search for anyway).

Use any of the working IEnumerable<string> answers (or at least, any which cover the range you need), and then if you need to create a list with a certain number of elements, just use:

List<string> list = GenerateSequence().Take(count).ToList();
like image 28
Jon Skeet Avatar answered Sep 19 '22 06:09

Jon Skeet