본문 바로가기

Algorithm/Baekjoon

[백준_JAVA] 1152 단어의 개수

반례가 존재하는 것 같다. 틀린 이유를 모르겠다.

 


import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String word = sc.nextLine().trim();
		if(word.isEmpty())
			System.out.println(0);
		else
			System.out.println(word.split(" ").length);
		sc.close();
	}
}

정답 코드이다.

 

trim과 split, isEmpty()를 이용하면 된다.

trim: 양쪽 끝의 공백을 제거한다.

split: 특정 문자를 기준으로 문자열을 나눈다.

isEmpty(): 빈 공백을 체크한다.

'Algorithm > Baekjoon' 카테고리의 다른 글