## Problem:

Given 2 strings, a and b, return a new string made of the first char of a and the last char of b, so "yo" and "java" yields "ya". If either string is length 0, use '@' for its missing char.

lastChars("last", "chars") → "ls"
lastChars("yo", "java") → "ya"
lastChars("hi", "") → "h@"

## Solution:

public String lastChars(String a, String b) {
if(a.length() == 0)
a = "@";
if(b.length() == 0)
b = "@";

return a.substring(0,1) + b.substring(b.length()-1, b.length());
}

1. you can just say "b.substring(b.length()-1)" on line 7 as one parameter in the substring method means to start at that index and go to the end of the string.

1. You're exactly right. Who wrote these solutions?

2. haha I am such a N00B!
public String lastChars(String a, String b) {
String result = "";
if(a.length() >= 2 && b.length() >= 2){
result = a.substring(0,1) + b.substring(b.length()-1);
}if(a.length() < 2 && b.length() >= 2){
result = "@" + b.substring(b.length()-1);
}if(a.length() >= 2 && b.length() <= 1){
result = a.substring(0,1) + "@";
}if(a.length() < 1 && b.length() < 1){
result = "@@";
}
return result;
}

3. public String lastChars(String a, String b) {
String output = "";
if (a.length() > 0) {
output += a.substring(0,1);
} else {
output += "@";
}

if (b.length() > 0) {
output += b.substring(b.length()-1);
} else {
output += "@";
}
return output;
}

4. Wow... I did a much more complicated version
if(a.length() > 0 && b.length() > 0)
return "" + a.charAt(0) + b.charAt(b.length() - 1);
else if(a.length() > 0 && b.length() == 0)
return "" + a.charAt(0) + new String(new char[1 - Math.min(b.length(), 1)]).replace("\0", "@");
else if(a.length() == 0 && b.length() > 0)
return "" + new String(new char[1 - Math.min(a.length(), 1)]).replace("\0", "@") + b.charAt(b.length() - 1);
return new String(new char[2]).replace("\0", "@");

5. I alrdy forgot, what i did right there, but yeah, it works at least...lol

public String lastChars(String a, String b) {

String z= "yo";
String u= "java";
String x= "ya";
String doe="@";

if(a.substring(0).equals(z)&&b.substring(0).equals(u))
return x;

if(a.length()==0&&b.length()!=0)
return doe + b.substring (b.length()-1);

if(a.length()!=0&&b.length()==0)
return a.charAt(0) + doe;

if(a.length()==0&&b.length()==0)
return doe+doe;

String s = a.charAt(0) + b.substring (b.length()-1);

return s;

}

6. public String lastChars(String a, String b) {
String sum="";
if(a.length()==0&&b.length()==0){
return "@"+"@";
}

if(a.length()==0){
sum=sum+ "@"+b.charAt(b.length()-1);
}
else if(b.length()==0){
sum=sum+ a.charAt(0)+"@";
}
else if (a.length()==0&&b.length()==0){
sum=sum+"@"+"@";
}
else if(a.length()!=0&&b.length()!=0){
sum=sum+a.charAt(0)+b.charAt(b.length()-1);
}
return sum;
}

7. if (a.length() == 0)
return "@" + a.substring(0, 1);
if (b.length() == 0)
return a.substring(0, 1) + "@";
if (b.length() == 0 && a.length() == 0)
return "@@";
else
return a.substring(0, 1) + b.substring(b.length() - 1, b.length());

8. public String lastChars(String a, String b) {
return a.length()>0&&b.length()>0?a.substring(0,1)+b.substring(b.length()-1):
a.length()==0&&b.length()!=0?"@"+b.substring(b.length()-1):
b.length()==0&&a.length()!=0?a.substring(0,1)+"@":"@@";
}

9. public String lastChars(String a, String b) {
if(a.length()<1 && b.length()<1){
return "@"+"@";
}if(a.length()>1 && b.length()>1){
return a.substring(0,1)+b.substring(b.length()-1,b.length());
}
if(a.length()<1 && b.length()>1){
return "@"+b.substring(b.length()-1,b.length());
}
if(a.length()>1 && b.length()<1){
return a.substring(0,1)+"@";
}if(a.length()==1 && b.length()>1){
return a.substring(0)+b.substring(b.length()-1);
}
return a+b;
}

10. public String lastChars(String a, String b) {
if(a.length()==0 && b.length()==0)
return "@"+""+"@";
else if(a.length()==0)
return "@"+""+b.charAt(b.length()-1);
else if(b.length()==0)
return a.charAt(0)+""+"@";
else
return a.charAt(0)+""+b.charAt(b.length()-1);
}

11. public String lastChars(String a, String b) {
if (a.length()>0 && b.length()>0 ){
return a.substring(0,1)+ b.substring(b.length()-1);
}
if (a.length()>0 && b.length()==0 ){
return a.substring(0,1)+ "@";
}
if (a.length()==0 && b.length()>0 ){
return "@"+ b.substring(b.length()-1);
}
else
return "@"+"@";
}

12. String first = "@";
String second = "@";

if(a.length() > 0) first = a.substring(0,1);
if(b.length() > 0) second = b.substring(b.length()-1);

return first+second;

13. In JavaScript, we can solve it, like this:

function lastChars(a, b){
if(a == '' && b == '') {
return '@' + '@'
} else if(b == 0) {
return a[0] + '@'
} else if(a == 0) {
return '@' + b[b.length - 1]
} else {
return a[0] + b[b.length - 1]
}
}

14. In JavaScript, we can solve it, like this:

function lastChars(a, b) {
if(a == '' && b == '') {
return '@' + '@'
} else if(b == 0) {
return a[0] + '@'
} else if(a == 0) {
return '@' + b[b.length - 1]
} else {
return a[0] + b[b.length - 1]
}
}