Write a method header on line two with the following specs:
Returns: a String Name: thirdLetter Parameters: a String called s |
Then complete the method by programming the following behavior
Returns every 3rd letter of the String s, starting the first letter. See below examples.
Examples:
- thirdLetter("hello there") ==> "hltr"
- thirdLetter("technology") ==> "thly"
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Main {
public static String thirdLetter(String s)
{
String result=String.valueOf(s.charAt(0));
for(int i=1;i<s.length();i++) {
if(i%3==0) {
result+=s.charAt(i);
}
}
return result;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(thirdLetter("hello there")); //"hltr"
System.out.println(thirdLetter("technology")); //"thly"
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
056 - Accumulator Method String Practice 4 (0) | 2019.12.26 |
---|---|
055 - Accumulator Method String Practice 3 (0) | 2019.12.26 |
053 - Accumulator Method String Practice 1 (0) | 2019.12.26 |
052 - Accumulator Method Challenge 2 (optional) (0) | 2019.12.26 |
051 - Accumulator Method Challenge 1 (optional) (0) | 2019.12.26 |