Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

Java
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 ?
Posted
Updated 29-Sep-16 22:38pm
v3
Comments
Richard MacCutchan 29-Sep-16 9:07am    
String[] splitString = (s.replaceAll("^[\\W+\\s+]", "").split("[\\s!,?._'@]+"));

Lines like the above are impossible to debug. Split it into separate statements so you can see the result of each part as yourt program runs.

1 solution

Finally after major changes, my code worked fine for all test cases.
Posting it here, if anyone in the future comes up with the same problem.

Java
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();
        if (s.trim().isEmpty()) {
			System.out.println(0);
        }
        else{
        String[] splitString = (s.replaceAll("^\\W+", "").split("[\\s!,?._'@]+"));
        System.out.println(splitString.length);
        for (String string : splitString) {
        	System.out.println(string);
        }
        }
    }
}
 
Share this answer
 
Comments
Maciej Los 30-Sep-16 4:40am    
Great. Mark it as answer (green button) to remove your question from unanswered list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900