Java Tokens problem on hackerrank solution

 Java Tokens problem on hackerrank 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().trim();
        // trim method is used to remove leading whitespace
        String[] tokens = s.split("[!,?._'@\\s]+");
        // [A-Za-z !,?._'@]+
        // //s is used to encounter whitespaces from the string
        if(s.length()>0)
        {
             System.out.println(tokens.length);
             for(String str:tokens)
            {
                System.out.println(str);
            }
        }
        else
        {
            System.out.println(0);
        }
        scan.close();
    }
}

Output:





Post a Comment

0 Comments