
/*
 * This class represents a collection of words.
 * It also supports the replaceMatches operation 
 * necesary to implement "censorship".
 */
public class Dictionary {

        private static final int MAX_SIZE = 1000;

        private String words[];

        // current size of dictionary
        private int count;

        public Dictionary() {
                words = new String[MAX_SIZE];
                count = 0;
        }

        // add a new word, provided that there is space.
        public void add(String s) {
                if (count < words.length) {
                        words[count] = s;         
                        count++;
                }
        }

        // replace all occurrences of each word
        // in the dictionary inside s with the
        // replacement string.
        public String replaceMatches(String text, String replaceWithText) {
                for (int i = 0; i < count; i++) {
                    
                                    String replace = "";
                                    for (i = 0 ; i < words[i].length(); i++) {
                                        replace = replace+"*";
                                    }
                        text = text.replaceAll(words[i], replace);
                }
                return text;
        }

        // returns the dictionary contents in a resonable form
        public String toString() {
                String res = "";
                for (int i = 0; i < count; i++) {
                        res = res + words[i] + "\n";
                }
                return res;
        }
}
