Created
June 20, 2014 08:24
-
-
Save gabhi/98ca24bbc328561b187d to your computer and use it in GitHub Desktop.
RemoveVowels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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