Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

string challenge without first and last

Posted on 2014-11-14
12
134 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
Work with App store 7 68
hibernate example using maven 12 57
Java basic valueOf question 1 29
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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