Write a method header on line two with the following specs:
Returns: a String Name: keepVowels Parameters: a String called s Purpose: create a string that is composed of all the vowels in the string, in the same order that they appear. see below examples for clarity. |
Examples:
- keepVowels("hello") ==> "eo"
- keepVowels("how do i internets") ==> "ooiiee"
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Main {
public static String keepVowels(String s)
{
char[] arr = {'a','e','i','o','u'};
s=s.toLowerCase();
String result="";
for(int i=0;i<s.length();i++) {
for(int j=0;j<arr.length;j++) {
if(s.charAt(i)==arr[j]) {
result+=arr[j];
}
}
}
return result;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(keepVowels("hello")); //eo
System.out.println(keepVowels("how do i internets")); //ooiiee
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
052 - Accumulator Method Challenge 2 (optional) (0) | 2019.12.26 |
---|---|
051 - Accumulator Method Challenge 1 (optional) (0) | 2019.12.26 |
049 - Accumulator Method Practice 5 (0) | 2019.12.26 |
048 - Accumulator Method Practice 4 (0) | 2019.12.26 |
047- Accumulator Method Practice 3 (0) | 2019.12.26 |