I am trying to write code that would double all vowels within a string. So if the string is hello, it would return heelloo. This is what I currently have:
public String doubleVowel(String str)
{
for(int i = 0; i <= str.length() - 1; i++)
{
char vowel = str.charAt(i);
if(vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u')
{
}
}
}
You can use a regular expression with a single call to String.replaceAll(String, String)
and your method might be static
because you don't need any instance state (also, don't forget upper case vowels). Something like
public static String doubleVowel(String str) {
return str.replaceAll("([AaEeIiOoUu])", "$1$1");
}
Where $1
matches the first (only) pattern grouping expressed in ()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With