Solved

string challenge without first and last

Posted on 2014-11-14
12
129 Views
Last Modified: 2014-11-30
Hi,

I am working on below coding challenge
http://codingbat.com/prob/p174254
i wrote as below


public String withouEnd2(String str) {
if(str.length()>=2){
return str.substring(1,str.length()-1);

}
return "";
 
}

My test cases are passing
i would like to know how can improve on my above code. For string length 1or 2 i returned "". Not sure if that is what challenge is expecting me to do.Please advise.Thanks in advance
0
Comment
Question by:gudii9
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40443539
return str.length()>2?str.substring(1,str.length()-1):"";
0
 
LVL 9

Accepted Solution

by:
arioh earned 167 total points
ID: 40444051
It's a good practice to test values for null
return str != null && str.length() > 2 ? str.substring( 1, str.length() - 1 ) : "";
0
 
LVL 7

Author Comment

by:gudii9
ID: 40444068
return str.length()>2?str.substring(1,str.length()-1):"";

Open in new window


does above one supposed to be as below with a = sign

return str.length()>=2?str.substring(1,str.length()-1):"";

Open in new window

please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40444178
when str.length() is 2, str.substring(1,str.length()-1) is "", so either one gives the same answer, but "" seems the more efficient way to handle that case.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 167 total points
ID: 40447585
>> Not sure if that is what challenge is expecting me to do
Yes it is. Since it states:
withouEnd2("ab") → ""

Open in new window

and in the tests are also included:
withouEnd2("a") → ""

Open in new window

and
withouEnd2("") → ""

Open in new window

So, why doubting?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40458912
when str.length() is 2, str.substring(1,str.length()-1) is "", so either one gives the same answer, but "" seems the more efficient way to handle that case.

Open in new window


can you please elaborate on this. I was not clear on this.
public String withouEnd2(String str) {
if(str.length()>=2){
return str.substring(1,str.length()-1);

}
return "";
  
}

Open in new window


my code passed all test cases including "ab" eventhough i used str.length()>=2 condition. I did not return "" for this right instead returning  str.substring(1,str.length()-1);

please advise
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
ID: 40458929
when str is "ab", str.substring(1,str.length()-1) is ""
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40464166
1. See the documentation of the substring method
It has this examples:
 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"

Open in new window

and also this explanation:
Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive.

When your string is "ab" it has a length of 2. So, in fact
str.substring(1,str.length()-1)

Open in new window

becomes
str.substring(1, 1);

Open in new window

That says, "take the substring that starts with the 2nd character, but not including the 2nd character".
Well, in that case you end up with nothing being the empty string "".

So writing

public String withouEnd2(String str) {
    if(str.length()>=2) {
      return str.substring(1,str.length()-1);
    }
    return "";
}

Open in new window

is functionally the same as writing

public String withouEnd2(String str) {
    if(str.length()>2) {
      return str.substring(1,str.length()-1);
    }
    return "";
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40467799
is functionally the same as writing
but if my string is "ab" for below code it wont enter into the if loop right since 2>2 is false
public String withouEnd2(String str) {
    if(str.length()>2) {
      return str.substring(1,str.length()-1);
    }
    return "";
}

Open in new window


Please advise
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 40468174
Yes,  2>2 is false so it would do  return "";

which would have the same result as return str.substring(1,str.length()-1);
both have the effect of withouEnd2("ab") → ""
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40470064
>> but if my string is "ab" for below code it wont enter into the if loop right since 2>2 is false
That's right.
So, it skips the if block and returns "".
And if it would have gone into the if block it would also return "".
So: it's functionally the same
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40473182
Thanx 4 axxepting
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now