Skip to content

UKSPC Practice Problems » Anagrams

The permutation of the letters of a word is called an anagram. For example, if the original word is FINALE, several anagrams are:

Write a program that reads one word per line from the input file, called anagram.in, until it reaches end of file. After reading a word from the input file, the program should write all the anagrams of that word (including the original word) to an output file, called anagram.out. The anagrams of each word should be separated by a line with only four hyphens (the '-' character) on it. (Note: the last line of the file is not "----".)

Be careful not to write the same word to the output twice. Each line of the input file will contain only one word from which to make anagrams. No line will contain more than 100 characters. For the purpose of this program, anagrams do not have to be valid English words.

Sample Input

to
pig

Sample Output

to
ot
----
pig
pgi
ipg
igp
gpi
gip

Adapted from CIL '96 - Round 3 - Set 1 - Problem 1.