Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!cmcl2!adm!news From: ECOAV%ECOSTAT.AAU.DK@uga.cc.uga.edu Newsgroups: comp.lang.pascal Subject: Re: Anagram Message-ID: <25442@adm.brl.mil> Date: 8 Jan 91 23:17:21 GMT Sender: news@adm.brl.mil Lines: 68 > Does anyone know of or know where I could find an algorithm for producing a > complete set of anagrams for for any string of length N? It seems (to my > nonscientific mind) that the algorithm changes as more letters are added. I was > able to produce one that worked for 3 and 4 letter strings, but after that it > broke down. The bookstore here on campus had a smattering of CSci "101 > algrotithms"-type books, but none mentioned algorithms for variable-length > (or any length, for that matter) anagrams. I know this sounds like a tedious > CSci assignment, but ever since I saw my first issue of _Spy_ and their > "Monthly Anagram Analysis" I've had the burning desire to find them the brute > force way. Reply here or to Email. Gracias. The following PASCAL program should do the job in an illustrative way: program test(input,output); const max_len = 10; type string = varying [max_len] of char; check = array [1..max_len] of boolean; var flag : check; line,anagram : string; ll,ptr : integer; procedure permute; var i : integer; begin if ptr>ll then begin writeln(anagram) end else begin for i:=1 to ll do begin if flag[i] then begin anagram[ptr]:=line[i]; flag[i]:=false; ptr:=ptr+1; permute; ptr:=ptr-1; flag[i]:=true; end; end; end; end; var i : integer; begin write('Enter string> '); readln(line); ll:=length(line); for i:=1 to ll do flag[i]:=true; anagram:=line; ptr:=1; permute; end. Note: This is VAX PASCAL. To convert to TURBO PASCAL substitute "varying [max_len] of char" with "string[max_len]". Arne Vajhxj ISIS08@ECOSTAT.AAU.DK