-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountWord.java
More file actions
38 lines (27 loc) · 801 Bytes
/
Copy pathCountWord.java
File metadata and controls
38 lines (27 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package quiz;
import java.util.Scanner;
public class CountWord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("문자열 입력");
String sentense = scanner.nextLine();
int count = 1;
StringBuilder builder = new StringBuilder();
builder.append(sentense);
if(builder.indexOf(" ") == 0) {
builder.delete(0, 1);
}else if(builder.lastIndexOf(" ") == builder.length()) {
builder.delete(builder.length()-1, 1);
}
for(int i = 0; i < builder.length(); i ++) {
char chr = builder.charAt(i);
String str = String.valueOf(chr);
if(str.equals(" ")) {
count += 1;
}
}
System.out.println(count);
}
}
}