/** * AlphabetUtil: This class implements some Alphabetic Utilities. * (primarily capitalize). * @version: 1.0 * @author: Sean Geraty * */ public class AlphabetUtil { static char[] alphaUpp = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; static char[] alphaLow = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; static char[] vowels = new char[] {'a','e','i','o','u'}; // array of lower case name prefixes - could be changed as required static String[] lowerPrefix = new String[] {"da","de","del","den","der","la","le","of","op","ten","ter","van","von"}; // private constructor - not to be instantiated private AlphabetUtil() { } /** * Application entrypoint - only used to test the capitalize method. * @param args[] the command line arguments */ public static void main(String args[]) throws Exception { if (args.length != 1) { System.out.println("USAGE: java AlphabetUtil word"); System.out.println("\tword : The Word Parameter (into capitalize method)."); System.exit(1); } System.out.println(capitalize(args[0])); } /** * Capitalizes a name. * @param name A name to capitalize. * @return A String containing the capitalized name. */ public static String capitalize(String name) { String tName = name.toLowerCase(); char c[] = tName.toCharArray(); boolean bStart = true; int iStart = 0; char u; for (int i = 0; i < c.length; i++) { if ((u = isAlphaLow(c[i])) != ' ') { // Start of a word if (bStart) { // Handle prefix in previous word - if found, reset to lower case if (i > iStart + 2 && isPrefix(tName.substring(iStart, i-1))) { c[iStart] = tName.charAt(iStart); } // Capitalize character and save the start location c[i] = u; iStart = i; bStart = false; } // Handle Mc... and Mac... - capitalize if appropriate else if ((i == iStart + 2) && (tName.substring(iStart, iStart + 2).equals("mc"))) { c[i] = u; } else if ((i == iStart + 3) && (tName.substring(iStart, iStart + 3).equals("mac"))) { if ((c[i] == 'i') || (!((c[i] == 'c') || (c[i] == 'k') || isVowel(c[i])))) { c[i] = u; } } } // Non-alpha found - set start of next word indicator else { bStart = true; } } return String.valueOf(c); } /** * Checks if a character is a lowercase alphabetic. * @param x The char to check. * @return A char containing an uppercase character or space (for non-alpha). */ public static char isAlphaLow(char x) { for (int j = 0; j < alphaLow.length; j++) { if (x == alphaLow[j]) { return alphaUpp[j]; } } return ' '; } /** * Checks a string for a name prefix. * @param tWord The String to check. * @return true for a match. */ public static boolean isPrefix(String tWord) { for (int j = 0; j < lowerPrefix.length; j++) { if (tWord.equals(lowerPrefix[j])) { return true; } } return false; } /** * Checks if a character is a vowel. * @param x The char to check. * @return true if the character is a vowel. */ public static boolean isVowel(char x) { for (int j = 0; j < vowels.length; j++) { if (x == vowels[j]) { return true; } } return false; } }