import java.io.*; /* This program: - reads a dictionary from standard input, - computes the 'signature' of each word, and - prints both the word and its signature to standard output. A dictionary is simply a file of words, one word per line. We could use the signOf() method to implement an algorithm to find anagrams, as several of you demonstrated on Exam 1. On Mac OS X, you can run this program against the system dictionary using the following command: cat /usr/share/dict/words | java Sign | less On Linux, your system dictionary probably resides in a different location. */ public class Sign { public static void main( String[] args ) throws IOException { BufferedReader inputFile = new BufferedReader( new InputStreamReader(System.in) ); while( true ) { String word = inputFile.readLine(); if ( word == null ) break; word = word.toLowerCase(); System.out.println( signOf(word) + " " + word ); } } public static String signOf( String s ) { /* The signature of a word is a sorted version of itself. I use a selection sort on an array to put the characters of s in order. At the end of each pass through the i-loop, line[0..i] contain the smallest i+1 characters from line. */ char[] line = s.toCharArray(); int minLocation; char temp; for (int i = 0; i < line.length-1; i++) { minLocation = i; for (int j = i+1; j < line.length; j++) if ( line[j] < line[minLocation] ) minLocation = j; temp = line[minLocation]; line[minLocation] = line[i]; line[i] = temp; } return new String( line ); } }