Solved

string challenge middle characters

Posted on 2014-11-14
5
219 Views
Last Modified: 2014-11-14
Hi,

I am working on below coding challenge
http://codingbat.com/prob/p137729
i wrote as below
public String middleTwo(String str) {
  int len=str.length();
 
  if(len%2==0){
  return str.substring((len/2)-1,(len/2)+1);
  }
  return "";
}


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

Assisted Solution

by:MogalManic
MogalManic earned 250 total points
ID: 40443524
The first sentence is "Given a string of even length" so odd is not supported.

If odd was supported, you would have to make a decision on where to choose.  If you use the string "12345", then length is 5.  So 5/2=2 (integer math) with a remainder 1.  So do you return "34" or "23"?  That would have to be decided on before implementation could be written.
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40443532
public String middleTwo(String str) {
  int len=str.length()/2;
  return str.substring(len-1,len+1);
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40443582
public String middleTwo(String str) {
  int len=str.length()/2;
  return str.substring(len-1,len+1);
}

Open in new window

so the str is even not odd one right by default. That is the challenge assumption right i do not need to do like

  if(len%2==0){

Open in new window

please advise
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 40443689
Yes you do not need to do the len%2==0 check.  The challenge states that the string will always be  So all of the test cases pass and the return of "" will never be executed. be executed.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40443722
Even if the challenge did include odd length strings, one might wonder whether "" was the most sensible return value for an input of "12345".
Now if the challenge had included 0 length strings, then it may be appropriate to return "" after a test.
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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 55
troubleshoot a python script 8 70
Html split(text) 2 26
netbeans can not run self signed app 8 7
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

680 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