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()); }
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.
ReplyDeleteYou're exactly right. Who wrote these solutions?
Deletehaha I am such a N00B!
ReplyDeletepublic 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;
}
public String lastChars(String a, String b) {
ReplyDeleteString 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;
}
Wow... I did a much more complicated version
ReplyDeleteif(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", "@");
I alrdy forgot, what i did right there, but yeah, it works at least...lol
ReplyDeletepublic 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;
}
public String lastChars(String a, String b) {
ReplyDeleteString 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;
}
if (a.length() == 0)
ReplyDeletereturn "@" + 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());
public String lastChars(String a, String b) {
ReplyDeletereturn 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)+"@":"@@";
}
public String lastChars(String a, String b) {
ReplyDeleteif(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;
}
public String lastChars(String a, String b) {
ReplyDeleteif(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);
}
public String lastChars(String a, String b) {
ReplyDeleteif (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 "@"+"@";
}
String first = "@";
ReplyDeleteString second = "@";
if(a.length() > 0) first = a.substring(0,1);
if(b.length() > 0) second = b.substring(b.length()-1);
return first+second;
In JavaScript, we can solve it, like this:
ReplyDelete```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]
}
}```
In JavaScript, we can solve it, like this:
ReplyDeletefunction 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]
}
}