Solved

string challenge middle characters

Posted on 2014-11-14
5
234 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
[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
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

691 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