Spaces:
Running
Running
Create 14. Remove vowels
Browse files
Week 2: Methods, strings and lists/14. Remove vowels
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
String removeVowels(String word)
|
| 4 |
+
|
| 5 |
+
which returns a string with all vowels (a, e, i, o, u and y) removed from the string given as a parameter.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
You can assume that the word is written entirely in lower case.
|
| 10 |
+
Example of a method call:
|
| 11 |
+
public static void main(String[] args) {
|
| 12 |
+
String testword = "welcome!";
|
| 13 |
+
System.out.println(removeVowels(testword));
|
| 14 |
+
}
|
| 15 |
+
Program outputs:
|
| 16 |
+
wlcm!
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
======================================
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
import java.util.Random;
|
| 24 |
+
|
| 25 |
+
public class Test{
|
| 26 |
+
public static void main(String[] args){
|
| 27 |
+
final Random r = new Random();
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
String[] words = "first second third fourth programming class method java variable".split(" ");
|
| 31 |
+
for (String word : words) {
|
| 32 |
+
System.out.println("Testing with parameter " + word);
|
| 33 |
+
System.out.println("Without vowels: " + removeVowels(word));
|
| 34 |
+
System.out.println("");
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
//ADD
|
| 41 |
+
public static String removeVowels(String word) {
|
| 42 |
+
String newWord = "";
|
| 43 |
+
|
| 44 |
+
// if the character is not a/e/i/o/u/y - then add to newWord
|
| 45 |
+
for (int i = 0; i < word.length(); i++) {
|
| 46 |
+
if (word.charAt(i) != 'a'
|
| 47 |
+
&& word.charAt(i) != 'e'
|
| 48 |
+
&& word.charAt(i) != 'i'
|
| 49 |
+
&& word.charAt(i) != 'o'
|
| 50 |
+
&& word.charAt(i) != 'u'
|
| 51 |
+
&& word.charAt(i) != 'y') {
|
| 52 |
+
newWord += word.charAt(i);
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
return newWord;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
Testing with parameter first
|
| 68 |
+
Without vowels: frst
|
| 69 |
+
|
| 70 |
+
Testing with parameter second
|
| 71 |
+
Without vowels: scnd
|
| 72 |
+
|
| 73 |
+
Testing with parameter third
|
| 74 |
+
Without vowels: thrd
|
| 75 |
+
|
| 76 |
+
Testing with parameter fourth
|
| 77 |
+
Without vowels: frth
|
| 78 |
+
|
| 79 |
+
Testing with parameter programming
|
| 80 |
+
Without vowels: prgrmmng
|
| 81 |
+
|
| 82 |
+
Testing with parameter class
|
| 83 |
+
Without vowels: clss
|
| 84 |
+
|
| 85 |
+
Testing with parameter method
|
| 86 |
+
Without vowels: mthd
|
| 87 |
+
|
| 88 |
+
Testing with parameter java
|
| 89 |
+
Without vowels: jv
|
| 90 |
+
|
| 91 |
+
Testing with parameter variable
|
| 92 |
+
Without vowels: vrbl
|
| 93 |
+
|
| 94 |
+
|