Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java String Tokens-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java String Tokens – Hacker Rank Solution
Java String Tokens – Hacker Rank Solution
Problem :
Given a string,8 , 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.
Input Format
A single string, 8.
Constraints
- 8 is composed of any of the following: English alphabetic letters, blank spaces, exclamation points (
!
), commas (,
), question marks (?
), periods (.
), underscores (_
), apostrophes ('
), and at symbols (@
).
Output Format
On the first line, print an integer,n, denoting the number of tokens in string 8 (they do not need to be unique). Next, print each of the n tokens on a new line in the same order as they appear in input string 8.
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
Explanation
We consider a token to be a contiguous segment of alphabetic characters. There are a total of such tokens in string , and each token is printed in the same order in which it appears in string .
Java String Tokens – Hacker Rank Solution
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); } } }