-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-string-tokens.java
More file actions
42 lines (37 loc) · 1.09 KB
/
java-string-tokens.java
File metadata and controls
42 lines (37 loc) · 1.09 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
// Happy Coding:)
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
// Write your code here.
String tokens = " !,?._@'";
ArrayList<String> list=new ArrayList<String>();
int tokenindex = 0;
for(int i =0; i<s.length(); i++)
{
for(int j = 0; j< tokens.length(); j++)
{
if(s.charAt(i) == tokens.charAt(j))
{
list.add(s.substring(tokenindex,i));
tokenindex = i+1;
}
}
}
list.add(s.substring(tokenindex));
for( Iterator<String> iterator=list.iterator();iterator.hasNext(); )
{
String value=iterator.next();
if(value.isEmpty())
{
iterator.remove();
}
}
System.out.println(list.size());
for(String str:list)
{
System.out.println(str);
}
}
}