Solved

string challenge without first and last

Posted on 2014-11-14
12
135 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 140
printf performancy 11 86
running on tomcat not jboss eap 7.0 3 33
Link failure 16 70
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

726 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