Monday, February 24, 2014

How to generate alphabet sequence A, B, C, ....., Z, AAA, AAB,....., ZZZ in C#

In some of the cases we use alphabet for sequence. If we have options less then 27 then   we can direct use A-Z alphabet but if the options are more then we have to generate sequence.

Check the below logic to generate the alphabet sequence like -     A, B, C, ....., Z, AAA, AAB,....., ZZZ  

 private void btnGenerate_Click(object sender, EventArgs e)
        {
            List<string> lstSquence = new List<string>();
           
            int i = 0;
            //Generating 1000 alphabet sequence
            while (i < 1000)
            {
                lstSquence.Add(GenerateSequence(i));
                i++;
            }
        }


        //Method to generate alphabet sequence
        static string GenerateSequence(int index)
        {
            string result = string.Empty;
            while (index > 0)
            {
                index = index - 1;
                result = char.ConvertFromUtf32(65+ index % 26) + result;
                index = index / 26;
            }
            return result;
        }

       

This logic will generate the alphabet sequence like -     A, B, C, ....., Z, AAA, AAB,....., ZZZ

No comments:

Post a Comment