


반례가 존재하는 것 같다. 틀린 이유를 모르겠다.
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' 카테고리의 다른 글
[백준_JAVA] 5622 다이얼 (0) | 2023.07.25 |
---|---|
[백준_JAVA] 2908 상수 (0) | 2023.07.22 |
[백준_JAVA] 2675 문자열 반복 (0) | 2023.07.14 |
[백준_JAVA] 10809 알파벳 찾기 (0) | 2023.07.14 |
[백준_JAVA] 11720 숫자의 합 (0) | 2023.07.14 |