Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions java/string-2/mixString.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@
* 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;
StringBuffer mix = new StringBuffer();
int len = (a.length() > b.length())? a.length(): b.length();
for(int i = 0; i < len; i++){
if(i < a.length()) mix.append(a.charAt(i));
if(i < b.length()) mix.append(b.charAt(i));
}
return mix.toString();
}