user5014677
user5014677

Reputation: 694

Increment based on the first numbers in a string

I really hope you understand what i'm trying to do ^^

So i have a function which is supposed to increment a number. I give a string to this function which is a string based on several number. For example myfunction("64");

My purpose is to increment this string like this:

641
642
643
...
6410
6411
6412
...
6421
6422
...
6431
6432
...
64110
64111
....
64220
...

or

641
6410
64100
6411
64111
64112
64113
..
64119
6412
64120
64121
...
and so on until 64999.

So far here is my logic:

procedure tForm1.IterateEveryPossibilities(c: String);
var
   cLength: Integer;
   i,j: Integer
   sI: String; 
begin
   cLength : Length(x); //This is the length of the string
   for i : cLength+1 to 5 do // maximum length of the number is 5. there's no number over 99999
   begin
      for j := 0 to 9 do // adding 0 , 1 , 2 , ... 9 to the number
      begin
        si := c + intToStr(j);
      end;
     C := sI;
   end;
  end;

I tried to get how much number I have to add and just adding 1,2,...9 aat the end. But it doesn't try the number like 11 12 13 it just goes 641 642.... 649 and then 6491 6492 6493, and don't tries 6410 6411.

Can you help me? I can't find the logic for this one :/

Thanks

Upvotes: 1

Views: 537

Answers (1)

Jens Borrisholt
Jens Borrisholt

Reputation: 6402

If I understod you question correct the this should do the trick for you :

function IterateEveryPossibilities(c: string): string;
const
  Min = 1;
  Max = 999;
var
  Buffer: TStringList;
  i: Integer;
begin
  Buffer := TStringList.Create;
  try
    Buffer.LineBreak := ' ';

    for i := Min to Max do
      Buffer.Add(c + IntToStr(i));

    Result := Buffer.Text;
  finally
    Buffer.Free;
  end;
end;

Upvotes: 3

Related Questions