Write a method header on line two with the following specs:
Returns: a String
Name: mixString
Parameters:
-
a String called s1
-
a String called s2
Then, starting on line 4, write code that will return the strings combined, one letter at a time, starting with s1. See example below for an example. You can assume that s1 and s2 are equal lengths:
s1 ==> "12345" s2 ==> "abcde" mixString(s1,s2) ==> "1a2b3c4d5e" |
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Main {
public static String mixString(String s1, String s2)
{
String temp ="";
for(int i=0;i<s1.length();i++) {
temp+=s1.substring(i,i+1)+s2.substring(i,i+1);
}
return temp;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(mixString("12345","abcde")); //should be 1a2b3c4d5e
System.out.println(mixString("howdy","hello")); //should be hhoewldlyo
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
047- Accumulator Method Practice 3 (0) | 2019.12.26 |
---|---|
045 - Accumulator Method Practice 1 (0) | 2019.12.19 |
043 - Method Header Practice 8 (0) | 2019.12.19 |
042 - Method Header Practice 7 (0) | 2019.12.19 |
041 - Method Header Practice 6 (0) | 2019.12.19 |