public class VowelExpert { /** Given a list of target characters, find the (index of the) * first char in a word which is one of the targets. * @param word The word to find a vowel in. * @param target the list of characters we are looking for. * @return the index of the first target char in `word`, or (if no * targets occurs) the first index past the end of the String. */ int firstCharFrom( String word, String target ) { int i = 0; boolean seenATargetChar = false; while (i < word.length() && !seenATargetChar) { if (target.contains(word.substring(i,i+1))) { seenATargetChar = true; } ++i; } return i; } /** Find the position of the letter 'A' in a word. * @param word The word to find a vowel in. * @return the index of the first letter 'A' in `word` (upper or lower * case), or (if no 'A' occurs) the first index past the end of the String. */ public int firstA( String word ) { return firstCharFrom( word, "Aa" ); } /** Find the position of the first vowel in a word. * @param word The word to find a vowel in. * @return the index of the first vowel in `word`, or (if no * vowel occurs) the first index past the end of the String. */ public int firstVowel( String word ) { return firstCharFrom( word, VOWELS.toLowerCase()+VOWELS.toUpperCase() ); } /** Translate a word to Pig Latin. * @param word The word to translate. * @return `word` translated into (some dialect of) Pig Latin. */ public String toPigLatin( String word ) { int firstV = firstVowel(word); int lastIndex = word.length(); String firstPart = word.substring(0,firstV); String rest = word.substring(firstV, lastIndex); return rest + firstPart + "ay"; } private static final String VOWELS = "AE10U"; }