HackerRank: Java String Tokens

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.

Note: You may find the String.split method helpful in completing this challenge.

Sample Input

He is a very very good boy, isn't he?

Sample Output

10

He

is

a

very

very

good

boy

isn

t

he 


Java Code:

import java.util.*;

public class javaStringTokens {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String a[] = s.trim().split("[^a-zA-Z]+");
        if(s.trim().length()==0)
        System.out.println(0);
        else
        {
            System.out.println(a.length);
            for(String i:a)
            {
                System.out.println(i);
            }
        }
        scan.close();
    }
}

No comments:

Post a Comment

HTML Forms - Input Types - Cheat Sheet

Popular Posts