Java > String-1 > endsLy (CodingBat Solution)

Problem:

Given a string, return true if it ends in "ly".

endsLy("oddly") → true
endsLy("y") → false
endsLy("oddy") → false


Solution:

public boolean endsLy(String str) {
  int len = str.length();
  String ly = "ly";
 
  if (len < 2)
    return false;
  else if (ly.equals(str.substring(len-2,len)))
    return true;
  else
    return false;
}


14 comments :

  1. This below code will also work.

    public boolean endsLy(String str) {
    if(str.length() <2 ){
    return false;
    }
    if(str.substring(str.length()-2).equals("ly"))
    return true;
    else
    return false;
    }

    ReplyDelete
  2. public boolean endsLy(String str) {
    if(str.length() < 2){
    return false;
    }
    return(str.substring(str.length()-2).equals("ly"));
    }

    ReplyDelete
  3. public boolean endsLy(String str) {
    return str.endsWith("ly");
    }

    ReplyDelete
  4. public boolean endsLy(String str) {
    int len = str.length();

    return (len>=2) && str.substring(len-2, len).equals("ly");

    }

    ReplyDelete
  5. public boolean endsLy(String str) {
    if(str.length()<2){
    return false;
    }else{
    String a=str.substring(str.length()-2);
    String ly="ly";
    if(ly.equals(a)){
    return true;
    }return false;
    }
    }

    ReplyDelete
  6. public boolean endsLy(String str) {
    if(str.length()<2){
    return false;
    }else{
    String a=str.substring(str.length()-2);
    String ly="ly";
    if(ly.equals(a)){
    return true;
    }return false;
    }
    }

    ReplyDelete
  7. if(str.length() < 2) return false;

    return (str.substring(str.length()-2).equals("ly"));

    ReplyDelete
  8. public boolean endsLy(String str) {
    return (str.length()>1&&str.substring(str.length()-2,str.length()).equals("ly"));
    }

    ReplyDelete
  9. public boolean endsLy(String str) { if(str.length()>=2) { if(str.substring(str.length()-2,str.length())==("ly")) { return true; } else { return false; } } else { return false; } }

    ReplyDelete
  10. what is the error..!explain in detail

    ReplyDelete
  11. public boolean endsLy(String str) {

    int length=str.length();
    if(length<2)
    return false;
    if(str.charAt(length-1)=='y' && str.charAt(length-2)=='l')
    return true;
    else
    return false;
    }

    ReplyDelete
  12. public boolean endsLy(String str) {
    if(str.endsWith("ly")){
    return true;
    }else return false;
    }

    ReplyDelete
  13. public boolean endsLy(String str) {
    int len=str.length()-2;
    if(str.length()<2) return false;
    String s=str.substring(len);
    if(s.equals("ly")){
    return true;
    }
    return false;
    }

    ReplyDelete
  14. return str.endsWith("ly") ? true:false;

    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