Hacker rank java 19th problem solution

19.JAVA String Tokens



import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        /* Read input */
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        scan.close();
        
        s = s.trim(); // so that .split() works properly
        
        /* Check special cases */
        if (s.length() == 0) {
            System.out.println(0);
            return;
        }
        
        /* Split on all non-alphabetic characters */
        String [] words = s.split("[^a-zA-Z]+");
        
        /* Print output */
        System.out.println(words.length);
        for (String word : words) {
            System.out.println(word);
        }
    }
}

Compiler Message
Success
Input (stdin)Download
He is a very very good boy, isn't he?
{-truncated-}
Download to view the full testcase
Expected OutputDownload
10
He
is
a
very
very
good
boy
isn
t
he
{-truncated-}
Download to view the full testcase

Comments

Popular posts from this blog

Amazon Web Services

Hacker Rank all java and python problem solutions

Google Code-In mentorship experience :)