JAVA
BAEKJOON(백준) 문자열 - 1152번 JAVA CODE
violetisme
2023. 1. 13. 17:32
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String str = new String();
String[] word;
//단어의 수를 count해줄 정수형 변수
int count = 0;
//문자열 입력받기
str = s.nextLine();
//띄어쓰기 기준으로 문자열 해체
word = str.split(" ");
//해체했을 때 띄어쓰기가 여러개 포함되어있으면, 단어의 수로 카운트 될 수가 있음
for(int i=0; i<word.length; i++) {
//단어가 띄어쓰기(공백)가 아니면, 단어의 수 증가
if(word[i] != "") {
count++;
}
}
System.out.print(count);
}
}