-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaStringTokens.java
More file actions
52 lines (43 loc) · 1 KB
/
Copy pathJavaStringTokens.java
File metadata and controls
52 lines (43 loc) · 1 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*Given a string s: Split the string into words (tokens).
A token contains only English letters (A-Z or a-z).
Symbols like ! , ? . _ ' @ and spaces are separators.
Print :Number of tokens Each token on a new line
Example
Input:
He is a very very good boy, isn't he?
Tokens:
He
is
a
very
very
good
boy
isn
t
he
Total Tokens:
10*/
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Read complete input string
String s = scan.nextLine().trim();
// If string is empty
if (s.length() == 0) {
System.out.println(0);
scan.close();
return;
}
// Split string using given separators
String[] arr = s.split("[ !,?._'@]+");
// Print number of tokens
System.out.println(arr.length);
// Print each token on a new line
for (String str : arr) {
System.out.println(str);
}
scan.close();
}
}