/*Christopher Piette *La Salle Academy *February 9, 2010 *Intermediate Division 3 *Contest 2 *ACSL NYSIIS */ import java.util.Scanner; public class ACSLNYSIISPiette { public static void main(String [] args) { Scanner reader = new Scanner(System.in); for(int i = 1; i <= 5; i++) // 5 lines of input { System.out.print("Please enter string #" + i + ": "); String name = reader.nextLine(); name = newStart(name, "MAC", "MC"); name = newStart(name, "KN", "N"); name = newStart(name, "K", "C"); name = newStart(name, "PH", "F"); name = newStart(name, "PF", "F"); name = newStart(name, "SCH", "S"); name = newEnd(name,"EE", "Y"); name = newEnd(name,"IE", "Y"); name = newEnd(name,"DT", "D"); name = newEnd(name,"RT", "D"); name = newEnd(name,"RD", "D"); name = newEnd(name,"NT", "D"); name = newEnd(name,"ND", "D"); char first = name.charAt(0);// This preserves the first letter. name = replace(name, "EV", "AF"); name = first + name.substring(1); name = replace(name, "E", "A"); name = first + name.substring(1); name = replace(name, "I", "A"); name = first + name.substring(1); name = replace(name, "O", "A"); name = first + name.substring(1); name = replace(name, "U", "A"); name = first + name.substring(1); name = replace(name, "Q", "G"); name = first + name.substring(1); name = replace(name, "Z", "S"); name = first + name.substring(1); name = replace(name, "M", "N"); name = first + name.substring(1); name = replace(name, "KN", "N"); name = first + name.substring(1); name = replace(name, "K", "C"); name = first + name.substring(1); name = replace(name, "SCH", "S"); name = first + name.substring(1); name = replace(name, "PH", "F"); name = first + name.substring(1); for(int c = 0; c < name.length() - 1; c++) // H's followed by nonvowels { // are removed. if(name.charAt(c) == 'H' && name.charAt(c+1) != 'A') name = name.substring(0,c) + name.substring(c+1); } for(int d = name.length() - 1; d > 0; d--) // H's preceded by nonvowels { // are removed. if(name.charAt(d) == 'H' && name.charAt(d-1) != 'A') name = name.substring(0, d) + name.substring(d+1); } for(int e = name.length() - 1; e > 0; e--) // W's preceded by vowels { // are removed. if(name.charAt(e) == 'W' && name.charAt(e-1) == 'A') name = name.substring(0, e) + name.substring(e+1); } for(int q = name.length() - 1; q > 0; q--) // Repeats are fixed. { if(name.charAt(q) == name.charAt(q-1)) // All repeats are removed { // and replaced with a single // instance of the repeated name = name.substring(0,q) + name.substring(q+1); // letter. } } name = newEnd(name,"S", ""); name = newEnd(name,"AY", "Y"); name = newEnd(name, "A", ""); System.out.println("The translated name is " + first + name.substring(1)); System.out.println(); } } // Given a string, this method will replace a group of letters with another group public static String replace(String str, String target, String replacement) { if(str.indexOf(target) == -1) // If the target is not contained within the string, or is the first letter, return str; // we don't touch it. while(str.indexOf(target)>= 0) // All occurrences of the target are removed. str =str.substring(0, str.indexOf(target)) + replacement + str.substring(str.indexOf(target) + target.length()); return str; //If the target is in the middle, it is removed and swapped with different letters. } public static String newStart(String str, String target, String replacement) // This method is based on "replace." { // It swaps the first few letters of a name with something else. if(str.indexOf(target) == 0) // If the target is not contained within the string, it isn't changed at all. str = replacement + str.substring(target.length()); return str; } public static String newEnd(String str, String target, String replacement) // Based on "replace." { // Swaps the last few letters of a name with something else. if(str.lastIndexOf(target) < str.length() - target.length()) return str; str = str.substring(0, str.length() - target.length()) + replacement; return str; } }