?
Solved

string challenge without first and last

Posted on 2014-11-14
12
Medium Priority
?
140 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
[X]
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
  • 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 668 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

743 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