Problem:
Given a string of odd length, return the string length 3 from its middle, so "Candy" yields "and". The string length will be at least 3.
middleThree("Candy") → "and"
middleThree("and") → "and"
middleThree("solving") → "lvi"
Solution:
public String middleThree(String str) { int len = str.length() / 2; return str.substring(len-1,len+2); }
return str.substring(str.length() / 2 - 1, str.length() / 2 + 2);
ReplyDeletepublic String middleThree(String str) {
ReplyDeleteif(str.length()<4){
return str;
}else{
return str.substring(((str.length())/2)-1,(str.length()/2)+2);
}
}
public String middleThree(String str)
ReplyDelete{
int mid=str.length()/2;
return str.substring(mid-1,mid+2);
}