Java > String-1 > lastChars (CodingBat Solution)

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());
}


9 comments :

  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.

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

      Delete
  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;
    }

    ReplyDelete
  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;
    }

    ReplyDelete
  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", "@");

    ReplyDelete
  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;

    }

    ReplyDelete
  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;
    }

    ReplyDelete
  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());

    ReplyDelete
  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)+"@":"@@";
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact