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


4 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

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