/** * @author Sebastian Rühl * @version 0.1 * @category nonsense * * KennyTranslator -- a kennyfier
* Copyright 2002 Kohan Ikin
* Ported to JAVA by Sebastian Rühl
* * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class KennyTranslator { /** * The KENNYLETTERS in alphabetical order. Big Letters are the Same with the * only difference That the First char is UpperCase */ public static final String[] KENNYLETTERS = { "mmm", "mmp", "mmf", "mpm", "mpp", "mpf", "mfm", "mfp", "mff", "pmm", "pmp", "pmf", "ppm", "ppp", "ppf", "pfm", "pfp", "pff", "fmm", "fmp", "fmf", "fpm", "fpp", "fpf", "ffm", "ffp" }; private char translateKennyLetterToNormalLetter(String s) throws Exception { char[] input = s.toCharArray(); boolean bigChar = (input[0] == Character.toUpperCase(input[0])); input[0] = Character.toLowerCase(input[0]); String tmp = new String(input); for (int i = 0; i < KENNYLETTERS.length; i++) { if (tmp.equals(KENNYLETTERS[i])) { return bigChar ? (char) (i + 'A') : ((char) (i + 'a')); } } throw new Exception(s + " is no Kenny Word!!!"); } private String translateKennyToNormal(String s) { String result = ""; stringiterate: for (int i = 0; i < s.length(); i = i + 3) { while (Character.toUpperCase(s.charAt(i)) != 'M' && Character.toUpperCase(s.charAt(i)) != 'P' && Character.toUpperCase(s.charAt(i)) != 'F') { result = result + s.charAt(i); i++; if (i == s.length()) { break stringiterate; } } try { result = result + translateKennyLetterToNormalLetter(s.substring(i, i + 3)); } catch (Exception e) { e.printStackTrace(); } } return result; } private String translateNormalLetterToKennyLetter(char c) { if (c == Character.toUpperCase(c)) { char[] result = KENNYLETTERS[c - 'A'].toCharArray(); result[0] = Character.toUpperCase(result[0]); return new String(result); } else { return KENNYLETTERS[c - 'a']; } } private String translateNormalToKenny(String s) { String result = ""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z') result = result + c; // Ordered according to optimisations suggested by Stuart else { result = result + translateNormalLetterToKennyLetter(c); } } return result; } private boolean isKenny(String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if ((c >= 'a' && c <= 'z' && c != 'm' && c != 'f' && c != 'p') || (c >= 'A' && c <= 'Z' && c != 'M' && c != 'F' && c != 'P')) { return false; } } return true; } /** * @param s * String to Translate * @return translated String */ public String translate(String s) { if (isKenny(s)) { return translateKennyToNormal(s); } else { return translateNormalToKenny(s); } } /** * @param args * String to Translate */ public static void main(String[] args) { if (args.length < 1) { System.err.println(showUsage()); System.exit(1); } else { String toTranslate = args[0]; for (int i = 1; i < args.length; i++) { toTranslate = toTranslate + ' ' + args[i]; } System.out.println(new KennyTranslator().translate(toTranslate)); } } /** * @return the Usage of Programm */ public static String showUsage() { return "kennytranslator text-to-translate"; } }