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
A string is an array of char with some extra bells and whistles added.
So an
array of string
is a lot like anarray of array of char
.If you want to reverse the string, you'll have to access every char and reverse it.
Some tips:
M
but declare a local variable instead.AStr:= AStr + AChar
in a loop, if you can avoid it. If you know how long the result is going to be use theSetLength
trick as shown in the code. It's generates much faster code.Sleep
you can use aReadLn
to halt a console app. It will continue as soon as you press a key.writeln
in your working routine.array of string
in a parameter definition is an open array; a different thing from a dynamic array.T
,K
, etc are usually used for generic types, you shouldn't use them for normal variables; Use a descriptive name instead.