In this HackerRank Java String Tokens problem, we have given a string s, matching the regular expression and we need to split the string into tokens and then print the number of tokens followed by each token on a new line.

HackerRank Java String Tokens problem solution


HackerRank Java String Tokens problem solution

import java.util.*;

class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine().trim();
        scan.close();
        
        String[] tokens = input.split("[^\\p{Alpha}]+");
        
        int size = (input.isEmpty()) ? 0 : tokens.length;
        System.out.println(size);
        
        for(String s : tokens) {
            System.out.println(s);
        }
    }
}