Write a method header on line two with the following specs:
|
Returns: an integer Name: countA Parameters: a String called s Purpose: count the number of occurrences of 'a' or 'A' within s |
Examples:
- countA("aaa") ==> 3
- countA("aaBBdf8k3AAadnklA") ==> 6
Hint: How do you write a for loop to loop through every letter of a string? You've done this multiple times already :)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Main {
public static int countA(String s)
{
int cnt=0;
s=s.toLowerCase();
for(int i=0;i<s.length();i++) {
if(s.charAt(i)=='a') {
cnt++;
}
}
return cnt;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(countA("aaA")); //3
System.out.println(countA("aaBBdf8k3AAadnklA")); //6
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
| 050 - Accumulator Method Practice 6 (0) | 2019.12.26 |
|---|---|
| 049 - Accumulator Method Practice 5 (0) | 2019.12.26 |
| 047- Accumulator Method Practice 3 (0) | 2019.12.26 |
| 045 - Accumulator Method Practice 1 (0) | 2019.12.19 |
| 044 - Method Header Challenge 1 (Optional) (0) | 2019.12.19 |