1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package getmiddle;
public class StringExercise { String getMiddle(String word) { if(word.length() % 2 == 0) { word = word.substring((word.length()/2)-1, word.length()/2+1); }else { word = word.substring(word.length()/2, (word.length()/2)+1); } return word; }
public static void main(String[] args) { StringExercise se = new StringExercise(); System.out.println(se.getMiddle("poweer")); } }
|