KaiquanMah's picture
Create 14. Remove vowels
07f7ed0 verified
Write the method
String removeVowels(String word)
which returns a string with all vowels (a, e, i, o, u and y) removed from the string given as a parameter.
You can assume that the word is written entirely in lower case.
Example of a method call:
public static void main(String[] args) {
String testword = "welcome!";
System.out.println(removeVowels(testword));
}
Program outputs:
wlcm!
======================================
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
String[] words = "first second third fourth programming class method java variable".split(" ");
for (String word : words) {
System.out.println("Testing with parameter " + word);
System.out.println("Without vowels: " + removeVowels(word));
System.out.println("");
}
}
//ADD
public static String removeVowels(String word) {
String newWord = "";
// if the character is not a/e/i/o/u/y - then add to newWord
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) != 'a'
&& word.charAt(i) != 'e'
&& word.charAt(i) != 'i'
&& word.charAt(i) != 'o'
&& word.charAt(i) != 'u'
&& word.charAt(i) != 'y') {
newWord += word.charAt(i);
}
}
return newWord;
}
}
Testing with parameter first
Without vowels: frst
Testing with parameter second
Without vowels: scnd
Testing with parameter third
Without vowels: thrd
Testing with parameter fourth
Without vowels: frth
Testing with parameter programming
Without vowels: prgrmmng
Testing with parameter class
Without vowels: clss
Testing with parameter method
Without vowels: mthd
Testing with parameter java
Without vowels: jv
Testing with parameter variable
Without vowels: vrbl