forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixString.java
More file actions
26 lines (23 loc) · 814 Bytes
/
mixString.java
File metadata and controls
26 lines (23 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Given two strings, A and B, create a bigger string made of the first char
* of A, the first char of B, the second char of A, the second char of B,
* and so on. Any leftover chars go at the end of the result.
*/
public String mixString(String a, String b) {
char[] arr;
String end;
int count = 0;
if(a.length() < b.length()) {
arr = new char[2 * a.length()];
end = b.substring(a.length());
} else {
arr = new char[2 * b.length()];
end = a.substring(b.length());
}
for(int i = 0; i < arr.length / 2; i++) {
arr[count] = a.charAt(i);
count++;
arr[count] = b.charAt(i);
count++;
}
return new String(arr) + end;
}