Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created June 20, 2014 08:24
Show Gist options
  • Save gabhi/98ca24bbc328561b187d to your computer and use it in GitHub Desktop.
Save gabhi/98ca24bbc328561b187d to your computer and use it in GitHub Desktop.
RemoveVowels
public class RemoveVowels {
/*
* removeVowels - a recursive method that returns
* string formed by removing all lower-case vowels
* (a, e, i, o, u) from the String str.
*/
public static String removeVowels(String str) {
// base cases
if (str == null) {
return null;
}
if (str.equals("")) {
return "";
}
// recursive case
// Make a recursive call to remove vowels from the
// rest of the string.
String removedFromRest = removeVowels(str.substring(1));
// If the first character in str is a vowel,
// we don't include it in the return value.
// If it isn't a vowel, we do include it.
char first = str.charAt(0);
if (first == 'a' || first == 'e' || first == 'i'
|| first == 'o' || first == 'u') {
return removedFromRest;
} else {
return first + removedFromRest;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment