Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse strings in an array

procedure ReverseArray(var A : array of string);
var I,J,L : integer;
begin
  for I := Low(A) to High(A) do
  begin
    L := length(A[I]);
    for J := L downto 1 do M := M + A[I];
  end;
  writeln(M);
end;


begin
  for I := 1 to 4 do readln(T[I]);
  ReverseArray(T);
  sleep(40000);
end.

What I'm trying to do here basically is reverse every string in the array but I'm unable to do it , what the code above do is basically repeat the words depends on their length (I write 'bob' in the array , the procedure will give me 'bob' three times because the length is 3) ... not sure why it's not working properly and what I'm missing

like image 629
user2779177 Avatar asked Feb 07 '23 20:02

user2779177


2 Answers

Delphi has a ReverseString() function in the StrUtils unit.

uses
  StrUtils;

type
  TStrArray = array of string;

procedure ReverseArray(var A : TStrArray);
var
  I: integer;
begin
  for I := Low(A) to High(A) do
    A[I] := ReverseString(A[I]);
end;

var
  T: TStrArray;
  I: Integer
begin
  SetLength(T, 4);
  for I := 1 to 4 do Readln(T[I]);
  ReverseArray(T);
  ...
end.
like image 138
Remy Lebeau Avatar answered Feb 09 '23 09:02

Remy Lebeau


A string is an array of char with some extra bells and whistles added.
So an array of string is a lot like an array of array of char.
If you want to reverse the string, you'll have to access every char and reverse it.

procedure ReverseArray(var A : array of string);
var 
  i,j,Len : integer;
  B: string;
begin
  for i := Low(A) to High(A) do begin
    Len := length(A[i]);
    SetLength(B, Len);  //Make B the same length as A[i].
    //B[Len] = A[i][1]; B[Len-1]:= A[i][2] etc...
    for j := Len downto 1 do B[j]:= A[i][(Len-J)+1];
    //Store the reversed string back in the array.
    A[i]:= B; 
    //Because A is a var parameter it will be returned.
    //Writeln(B); //Write B for debugging purposes.
  end;
end;


var
  i: integer;
  Strings: array [0..3] of string;
begin
  for i := 0 to 3 do readln(Strings[i]);
  ReverseArray(Strings);
  for i := 0 to 3 do writeln(Strings[i]);
  WriteLn('Done, press a key...'); 
  ReadLn;
end.

Some tips:

  • Do not use global variables like M but declare a local variable instead.
  • Don't do AStr:= AStr + AChar in a loop, if you can avoid it. If you know how long the result is going to be use the SetLength trick as shown in the code. It's generates much faster code.
  • Instead of a Sleep you can use a ReadLn to halt a console app. It will continue as soon as you press a key.
  • Don't put the writeln in your working routine.
  • Note the first element in a string is 1, but the first element in a array is 0 (unless otherwise defined); Dynamic arrays always start counting from zero.
  • Note that array of string in a parameter definition is an open array; a different thing from a dynamic array.
  • Single uppercase identifiers like T, K, etc are usually used for generic types, you shouldn't use them for normal variables; Use a descriptive name instead.
like image 37
Johan Avatar answered Feb 09 '23 09:02

Johan