
/**
 * A collection of static methods that manipulate strings.
 *   @author Dave Reed 
 *   @version 8/15/13
 */
public class StringUtils {
    /**
     * Capitalizes a string.
     *   @param str an arbitrary string of characters
     *   @return a copy of str with the first character capitalized
     */
    public static String capitalize(String str) {
        if (str.length() == 0) {
            return str;
        }
        else {
            return Character.toUpperCase(str.charAt(0)) + str.substring(1, str.length());
        }
    }
    
    /**
     * Selects a random character from a string.
     *   @param str an arbitrary string of letters
     *   @return a character chosen at random from str
     */    
    public static char randomChar(String str) {
        Die d = new Die(str.length());
        return str.charAt(d.roll()-1);
    }
    
    /**
     * Determines whether a character is a vowel.
     *   @param ch an arbitrary character
     *   @return true if ch is in "aeiouAEIOU"
     */    
    public static boolean isVowel(char ch) {
        String vowels = "aeiouAEIOU";
        return vowels.contains(""+ch);
    }
    
    /**
     * Finds the first vowel (if any) in a string.
     *   @param str an arbitrary string of characters
     *   @return the index of the first voewl in str (or -1 if not found)
     */    
    public static int findVowel(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (StringUtils.isVowel(str.charAt(i))) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Translates a word into Pig Latin.
     *   @param word a string of characters (without spaces)
     *   @return the Pig Latin translation of that word
     */    
    public static String pigLatin(String word) {
        int firstVowel = StringUtils.findVowel(word);
        
        if (firstVowel <= 0) {
            return word + "way";
        }
        else {
            return word.substring(firstVowel, word.length()) + 
                   word.substring(0, firstVowel) + "ay";
        }
    } 
}
