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.
Input Format
A single string, s.
s is composed of English alphabetic letters, blank spaces, and any of the following characters: !,?._'@
Output Format
On the first line, print an integer,n, denoting the number of tokens in string s (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<br />
He<br />
is<br />
a<br />
very<br />
very<br />
good<br />
boy<br />
isn<br />
t<br />
he
What I have tried:
My Code:
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
scan.close();
String[] splitString = (s.replaceAll("^[\\W+\\s+]", "").split("[\\s!,?._'@]+"));
System.out.println(splitString.length);
for (String string : splitString) {
System.out.println(string);
}
}
}
This code works fine for the Sample Input but do not pass this test case.
Test case:
Input:
< >YES leading spaces are valid, problemsetters are evillllll
Expected Output:
8<br />
YES<br />
leading<br />
spaces<br />
are<br />
valid<br />
problemsetters<br />
are<br />
evillllll<br />
What changes in the code will pass this test case ?