BOJ/String
[BOJ] 1152번 단어의 개수
J_Jayce
2019. 8. 21. 19:23
문제
https://www.acmicpc.net/problem/1152
1152번: 단어의 개수
첫 줄에 영어 대소문자와 띄어쓰기로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 띄어쓰기 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다. 또한 문자열의 앞과 뒤에는 공백이 있을 수도 있다.
www.acmicpc.net
풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Main {
static Scanner scan = new Scanner(System.in);
static String test;
static int get_num_words() {
int answer = 0;
String[] arr = test.split(" ");
for(int i=0;i<arr.length;i++) {
if(arr[i].length()>0)
answer++;
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test = scan.nextLine();
System.out.println(get_num_words());
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|