Solved

string challenge middle characters

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 59
Python Assistance 7 35
Free Alternative to JIRA 4 55
VB Script to add site to Java Exception List 4 13
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 …
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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.

757 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

19 Experts available now in Live!

Get 1:1 Help Now