Java String Tokens Hackerrank Solution
For Explanation Check Video:
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
Code:
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(); s = s.trim(); if(s.length()==0){ System.out.println(0); return; } String[] words = s.split("[^a-zA-Z]+"); System.out.println(words.length); for(String word:words){ System.out.println(word); } scan.close(); }}
Comments
Post a Comment