프로그래머스 - 가운데 글자 가져오기
Contents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class StringExercise{
String getMiddle(String word){
int len = word.length();
if(len%2==0) {//짝수
return word.substring(len/2-1, len/2+1);
}
else {//홀수
return word.substring(len/2, len/2+1);
}
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args){
StringExercise se = new StringExercise();
System.out.println(se.getMiddle("power"));
}
} |
아래와 같은 풀이도 있다. 짝수와 홀수를 2로 나눴을때의 결과를 잘 활용한 것 같다. 배워야지
1 2 3 4 5 6 7 8 9 10 11 |
lass StringExercise{
String getMiddle(String word){
return word.substring((word.length()-1)/2, word.length()/2 + 1);
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args){
StringExercise se = new StringExercise();
System.out.println(se.getMiddle("power"));
}
} |
Author Jaejin Jang
LastMod 2017-12-23
License Jaejin Jang