Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi folks
I know it may seem quite stupid to most of the people here. But am stuck with a prblem
I want to write a programme which can create user id which consists of 3 letters and 6 digits e.G AAA000001 It should be the first no. . Progression should be like this AAA000001,AAA000002......... AAA999999,AAB000001........AAB999999 ....... AAZ999999 ,ABA000001........ AND so forth.Please anyone with suitable solution.Help me and share wid all of us.
Posted
Comments
Sergey Alexandrovich Kryukov 15-May-12 3:22am    
Do you really want to write this boring stuff, of someone wants you to write it?
Did you try anything on your own?
--SA

The UserId can be generated from a number using remainder and integer division as shown below:

C#
void Main()
{
    List<long> numbers = new List<long>{
        999999, 1000000, 1999999,
        1000000*26+1,
        1000000*26*26-1};
    foreach(long number in numbers)
        Console.WriteLine (GetId(number));
}

public string GetId(long number){
    long numberPart = number % 1000000;
    int charPos = (int)number / 1000000;

    return string.Format("{2}{1}{0}{3:000000}",
        GetChar(ref charPos),GetChar(ref charPos),
        GetChar(ref charPos),numberPart);
}
public char GetChar(ref int position){
    int ind = position % 26;
    position = position / 26;
    return (char)(ind+65);
}
//Output
//AAA999999
//AAB000000
//AAB999999
//ABA000001
//AZZ999999


I have not checked exhaustively. So, check the above code for your requirement and make modifications as found necessary.
 
Share this answer
 
v3
Comments
Sandeep Mewara 15-May-12 10:33am    
My 5!
VJ Reddy 15-May-12 10:41am    
Thank you, Sandeep :)
BillW33 15-May-12 10:43am    
Nicely done, 5 from me :)
VJ Reddy 15-May-12 10:49am    
Thank you, CIDev :)
Shahin Khorshidnia 15-May-12 12:50pm    
+5
Hi, this code here may not be completely correct or perfectly implemented, but should give you an idea how to do this:
C#
private void Form1_Load(object sender, EventArgs e)
{
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   m_nIntegerPart = 999998;
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   m_nIntegerPart = 999998;
   m_strStringPart = "AAZ";
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   m_nIntegerPart = 999998;
   m_strStringPart = "AZZ";
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
}

private int m_nIntegerPart = 0;
private string m_strStringPart = "AAA";

private string GetNewUserNumber()
{
   if (m_nIntegerPart < 999999)
      m_nIntegerPart++;
   else
   {
      if (m_strStringPart == "ZZZ")
         return ""; //all numbers were already used once
      m_nIntegerPart = 1;

      m_strStringPart = Increment(m_strStringPart);
   }

   return m_strStringPart + m_nIntegerPart.ToString("000000");
}

private static String Increment(String s)
{
   String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   char lastChar = s[s.Length - 1];
   string fragment = s.Substring(0, s.Length - 1);

   //Last character increment
   if (chars.IndexOf(lastChar) < 25)
   {
      lastChar = chars[chars.IndexOf(lastChar) + 1];
      return fragment + lastChar;
   }
   else
   {
      lastChar = 'A';
      char middleChar = s[s.Length - 2];
      fragment = s.Substring(0, s.Length - 2);
      if (chars.IndexOf(middleChar) < 25)
      {
         middleChar = chars[chars.IndexOf(middleChar) + 1];
         return fragment + middleChar + lastChar;
      }
      else
      {
         lastChar = 'A';
         middleChar = 'A';
         char firstChar = s[s.Length - 3];
         if (chars.IndexOf(firstChar) < 25)
         {
            firstChar = chars[chars.IndexOf(firstChar) + 1];
            return "" + firstChar + middleChar + lastChar;
         }
      }
   }

   return Increment(fragment) + '0';
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900