Solved

string challenge middle characters

Posted on 2014-11-14
5
201 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-position the objects 7 95
custom annotations 9 28
servlet  URL Rewriting 1 25
Windows 10 and JDK 7 7u85 4 31
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now