Problem: given a string (eg "abcd") print all the permutations
Algorithm:
Reword the algorithm:
Use a counter to record where to permutate from
Code:
size: constant integer:=4;
word: string(1..size);
procedure swapchar(S: in out word;a,b:integer) is
temp:character;
begin
temp:=S(a);
S(a):=S(b);
S(b):=temp;
end swapchars;
procedure permutate(S: in word; k:in integer) is
begin
if (k=S'LAST) then
put(S);
else
for i in k..S'LAST do
swapchars(S,k,i);
permute(S,k+1);
end if;
end permute;