Write a method header on line two with the following specs:
Returns: an integer Name: countVowels Parameters: a String called s Purpose: count the number of vowels in the string s. Assume s is all lowercase. |
Examples:
- countVowels("obama") ==> 3
- countVowels("happy friday! i love weekends") ==> 9
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Main {
public static int countVowels(String s)
{
char [] vowels = {'a','e','i','o','u'};
s=s.toLowerCase();
int cnt=0;
for(int i=0;i<s.length();i++) {
for(int j=0;j<vowels.length;j++) {
if(s.charAt(i)==vowels[j]) {
cnt++;
}
}
}
return cnt;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(countVowels("obama")); //3
System.out.println(countVowels("happy friday! i love weekends")); //9
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
051 - Accumulator Method Challenge 1 (optional) (0) | 2019.12.26 |
---|---|
050 - Accumulator Method Practice 6 (0) | 2019.12.26 |
048 - Accumulator Method Practice 4 (0) | 2019.12.26 |
047- Accumulator Method Practice 3 (0) | 2019.12.26 |
045 - Accumulator Method Practice 1 (0) | 2019.12.19 |