가운데 글자 가져오기 Level 1
https://programmers.co.kr/learn/challenge_codes/82
length와 length(), size()들의 차이점은 아래 링크에 잘 설명 되어 있다.
length와 length(), size()들의 차이점
즉 length는 배열, length()는 문자열, size는 콜렉션, 셋 객체에서 사용한다.
class StringExercise{
String getMiddle(String word){
return word.substring((word.length()-1)/2,word.length()/2+1) ;
//length()-1 후에 나누기 2 해야 함에 유의!!
//subString이 아니라 substring이다.
//substring(여기부터, 이 곳 전까지!)
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args){
StringExercise se = new StringExercise();
System.out.println(se.getMiddle("power"));
}
}
'프로그래밍 > 알고리즘(자바)' 카테고리의 다른 글
수박수박수박수박수박수? Level 1 #join #replace #StringBuffer #append #String '+=' 연산 (0) | 2018.05.06 |
---|---|
최대공약수와 최소공배수 Level 1 #유클리드 호제법 (0) | 2018.05.06 |
약수의 합 Level 1 (0) | 2018.05.06 |
최대값과 최소값 Level 1 (0) | 2018.05.06 |
피보나치 수 Level 1 (0) | 2018.05.06 |