tobiak777
tobiak777

Reputation: 3365

Incrementing a registration number composed of strings and letter

My problem is similar to that question but slightly different. I have a registration number having the form :

14AAA999 : 14 is the current year, each A represents a caracter and each 9 represents a digit. I know how to extract a number from a string but I would like to increment also the letters. Here is what I would like to achieve :

The first guy registered on the year 2014 would have this registration number : 14AAA000, the second 14AAA001, ..., until 14AAA999 and then it would become 14AAB000, and so on until 14ZZZ999. I'm looking for a good way to increment the letters for each overflow.

Thanks for your help !

Upvotes: 0

Views: 169

Answers (2)

rerun
rerun

Reputation: 25495

In your case you have a Number that is two separate bases. For the first 3 digits you are base 10 in the next base 26 where A = 0. you can simply create a number convert that converts from your representation to to other. This is slightly different than ISE solution.

class foo
{
    public static string ToRep(int Year,int Num)
    {
        StringBuilder SB = new StringBuilder(Year.ToString());
        SB.Append(":");
        int DecPart = Num % 1000;
        int Base26Part = Num / 1000;            
        for(int x = 0 ; x < 3 ; x++)
        {
            char NewChar =(char)( 'A'+ Base26Part % 26); 
            Base26Part /= 26;                
            SB.Insert(3,NewChar); 
        }
        SB.Append(DecPart.ToString("000"));
        return SB.ToString(); 
    }
}

Upvotes: 1

Pseudo-code:

char[] minValues { "14AAA000" }
char[] maxValues { "99ZZZ999" }

int index = input.Length - 1;

do
{
  if (input[index] < maxValues[index])
  {
    input[index]++;
    for (int j = index + 1; j < input.Length)
      input[j] := minValues[j];
    break;
  }
  else
    index--;
} while (true);

Upvotes: 1

Related Questions