Here is the condition I'm supposed to solve:
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.
Here is the working code I've managed to hatch after herculean efforts:
public String mixString(String a, String b) {
int cut = Math.abs(a.length() - b.length());
String end = "";
if (cut == 0){
for (int i = 0 ; i < a.length() ; i ++){
end = end + a.charAt(i) + b.charAt(i);
}
}
if (a.length() > b.length()){
for (int i = 0 ; i < b.length() ; i ++){
end = end + a.charAt(i) + b.charAt(i);
}
end += a.substring (a.length() - cut);
}
if (a.length() < b.length()){
for (int i = 0 ; i < a.length() ; i ++){
end = end + a.charAt(i) + b.charAt(i);
}
end += b.substring (b.length() - cut);
}
return end;
}
Thank you in advance.