Given a string, s, matching the regular expression [A-Za-z !,?._'@]+
, split the string into tokens. We define a token to be one or more consecutive English alphabetic letters. Then, print the number of tokens, followed by each token on a new line.
Note: You may find the String.split method helpful in completing this challenge.
Input Format
A single string,s .
Output Format
On the first line, print an integer,n , denoting the number of tokens in string  (they do not need to be unique). Next, print each of the n tokens on a new line in the same order as they appear in input string s.
Sample Input
He is a very very good boy, isn't he?
Sample Output
10
He
is
a
very
very
good
boy
isn
t
he
SOLUTION
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
s = s.replace("'", " ");
s = s.replace("?", " ");
s = s.replace(",", " ");
s = s.replace(".", " ");
s = s.replace("_", " ");
s = s.replace("@", " ");
s = s.replace("!", " ");
List < String > stringsList = new ArrayList < > (Arrays.asList(s.split(" ")));
stringsList.removeIf(item - > item == null || "".equals(item));
System.out.println(stringsList.size());
for (String st: stringsList) {
if (!st.isEmpty()) {
System.out.println(st);
}
}
// Write your code here.
scan.close();
}
}
FOLLOW FOR MORE QUESTIONS AND SOLUTIONS |Â DIGIT WOOD
Leave a Reply