Write a method header on line two with the following specs:
Returns: a String Name: spaceOut Parameters: a String called s |
Then complete the method by programming the following behavior
Insert spaces after every character in the String s, then return the new string.
See below examples (note the space at the end as well).
Examples:
- spaceOut("hello") ==> "h e l l o "
- spaceOut("technology") ==> "t e c h n o l o g y "
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Main {
public static String spaceOut(String s)
{
String result ="";
for(int i=0;i<s.length();i++)
{
result+=s.charAt(i)+" ";
}
return result;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(spaceOut("hello")); //"h e l l o "
System.out.println(spaceOut("technology")); //"t e c h n o l o g y "
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
055 - Accumulator Method String Practice 3 (0) | 2019.12.26 |
---|---|
054 - Accumulator Method String Practice 2 (0) | 2019.12.26 |
052 - Accumulator Method Challenge 2 (optional) (0) | 2019.12.26 |
051 - Accumulator Method Challenge 1 (optional) (0) | 2019.12.26 |
050 - Accumulator Method Practice 6 (0) | 2019.12.26 |