Beranda > Delphi > How the Losers Reverse String

How the Losers Reverse String

When you look in the Internet, you can find a lot various samples for beginners. This is great but not at all time.

As you know there is some article about reversing strings at almost every Delphi Tips. But what is wrong? -- The most widespread samples 'How to reverse string' is very stupid. I don't understand, why programmers left this thing without remark. So beginners copy down blunder his fathers and older 'practiced' programmers. The Firts function caled ReverseString is short, looks simply but it is the ugliest style of reverse. It is BFU and lOOser style.  The Second function ReverseStr looks intricately, but it is more sophisticated. And it is more 50x faster than 'classic solutions' (for string longer 1 kB). Look graph.

// Dont use this - it is really wrong and ugly
// It's 'classic' solutions, good for BFU only
function ReverseString(s : string) : string;
var
  i  : integer;
  s2 : string;
begin
  s2 := '';
  for i := 1 to Length( s ) do
  begin
    s2 := s[ i ] + s2; //this is silly
  end;
  Result := s2;
end;
// better way... looks complicated but 
// it is 50x faster!!! In graph as 'Our style'
// "... The First will be latest and the latest will be
// the first..." I suppose it said Our Lord :o)
function ReverseStr(InpStr : string) : string;
var
  half : integer;
  i,j  : integer;
  ch   : char;
begin
  Result := InpStr;
  j := length(Result);
  if j > 1 then
  begin
    half := j div 2;
    for i := 1 to half do
    begin
      ch := Result[i];        //We needn't create new
      Result[i] := Result[j]; //string. We shuffle    
      Result[j] := ch;        //chars of our old
      j := j-1;               //string 
    end;
  end;
end;
Reverse string in Delphi

 
// Another solution faster by 10proc. than ReverseStr.
// This funcion was written by Eric Grobler 
function Rev(const s : String) : String;
var
  i, j  : Integer;
  times : Integer;
begin
  j := Length(s);
  SetLength(Result, j);
  times := j div 2;
  if j mod 2<>0 then Inc(times);
  for i := 1 to times do
  begin
    Result[i] := s[j];
    Result[j] := s[i];
    Dec(j);
  end;
end;

// Example of test
procedure TForm1.Button1Click(Sender: TObject);
var i     : integer;
    t1,t2 : integer;
begin
  t1 := GetTickCount;
  for i := 1 to 10000 do
    Label1.Caption := RevertStr(Edit1.Text);
  t2 := GetTickCount;
  Label4.Caption := inttostr(t2-t1);

  t1 := GetTickCount;
  for i := 1 to 10000 do
    Label3.Caption := ReverseString(Edit1.Text);
  t2 := GetTickCount;
  Label5.Caption := inttostr(t2-t1);
end;

(rn)

Notice : String manipulations are very common. Thea are much more common in programs than special math funcions which are implemented in math CPU unit
(math coprocessor). So why aren’t there special strings-coprocessors?

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: