Link to home
Start Free TrialLog in
Avatar of smitty13
smitty13

asked on

Delphi Alogorithm Help

I am trying to write an algorithm to append a char. to the end of a string WITHOUT using the concat function...any ideas? it needs to be pretty quick and not use alot of memory.
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MyLength := Length(MyString);
SetLength(MyString, MyLength + 1);
MyString[MyLength] := MyChar;

would be an alternative method. But if you need something faster then you have to allocate a large buffer for the string and keep track of the current length of the string and the length of the buffer. Probably means declaring something like this:

var
  MyString: array of char;
  MyLength: Integer;
begin
  MyLength := 0;
  SetLength(MyString, 5000);
  repeat
   // This will just run forever, until you run out of memory.
    if (MyLength > High(MyString)) then SetLength(MyString, Length(MyString + 5000));
    MyString[MyLength] := 'a';
    MyLength := MyLength + 1;
  until false;
end;

It does add a little overhead but that just depends on the amount of space you allocate for the whole string buffer.
Avatar of CleanupPing
CleanupPing

smitty13:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Would like to see the points split between the 3 of us that commented on this question.

Just my 2 cents...
Russell