RemoveVowels.java

Download
1/*2 * RemoveVowels.java3 *4 * Computer Science S-225 *6 * A class that contains a recursive method for removing all7 * lower-case vowels that appear in a string.8 */9 10public class RemoveVowels {11    /*12     * removeVowels - a recursive method that returns 13     * string formed by removing all lower-case vowels14     * (a, e, i, o, u) from the String str.15     */16    public static String removeVowels(String str) {17        // base cases18        if (str == null) {19            return null;20        } else if (str.equals("")) {21            return "";22        } else {23            // recursive case24        25            // Make a recursive call to remove vowels from the26            // rest of the string.27            String removedFromRest = removeVowels(str.substring(1));28        29            // If the first character in str is a vowel,30            // we don't include it in the return value.31            // If it isn't a vowel, we do include it.32            char first = str.charAt(0);33            if (first == 'a' || first == 'e' || first == 'i' ||34              first == 'o' || first == 'u') {35                return removedFromRest;36            } else {37                return first + removedFromRest;38            }39        }40    }41}