Solved

string challenge without first and last

Posted on 2014-11-14
12
137 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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