How to get continuous characters?

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 677
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

Solution

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

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

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

Comments or Responses

Login to post response