Reverse strings in an array

2.2k Views Asked by At
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

5

There are 5 best solutions below

0
On BEST ANSWER

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.
1
On
uses
   System.SysUtils, System.StrUtils;

var
   Forwards, backwards : string;

begin
    forwards:= 'abcd';
    backwards:= ReverseString(forwards);
    Writeln(backwards);
    Readln;
end;

// dcba
0
On

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.
0
On

My preferred solution for this is

type
  TStringModifier = function(const s: string): string;

procedure ModifyEachOf( var aValues: array of string; aModifier: TStringModifier );
var
  lIdx: Integer;
begin
  for lIdx := Low(aValues) to High(aValues) do
    aValues[lIdx] := aModifier( aValues[lIdx] );
end;

and it ends up with

var
  MyStrings: array[1..3] of string;
begin
  MyStrings[1] := '123';
  MyStrings[2] := '456';
  MyStrings[3] := '789';

  ModifyEachOf( MyStrings, SysUtils.ReverseString );
end;
0
On

Come on! 'bob' is one of those words you shouldn't try to test a reverse routine. But the problem goes beyond that.

Your problem is in here

for J := L downto 1 do 
  M := M + A[I];

You are trying to add the whole string to the M variable instead of the character you are trying to access. So, it should be

for J := L downto 1 do 
   M := M + A[I][J];

Also you need to set M := '' inside the first loop where it will have nothing when you start accumulating characters in to it.

Third, move the writing part, WriteLn(M), inside the first loop where you get a nice, separated outputs.

Putting together, it is going to be:

for I := Low(A) to High(A) do
begin
    L := length(A[I]);

    M := '';        
    for J := L downto 1 do 
       M := M + A[I][J];

    writeln(M);
end;