The problem context involves taking a string input from the user and splitting it into tokens. Tokens are essentially parts of the string that are separated by certain characters. In this case, the characters are commas, spaces, apostrophes, underscores, question marks, and exclamation points.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution approach involves first checking if the string contains any alphabetic characters. If it does, the string is split into tokens using the specified characters as delimiters. The number of tokens and the tokens themselves are then printed. If the string does not contain any alphabetic characters, the program simply prints 0.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.strings;
import java.util.Scanner;
/**
*
*/
public class JavaStringTokens {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
scan.close();
boolean found = false;
for (int i = 0; i < s.length(); i++) {
int t = s.charAt(i);
if (65 <= t && t <= 90 || t >= 97 && t <= 112) {
found = true;
break;
}
}
if (found) {
String[] str = s.split("[, '@_.?!]+");
int length = str.length;
if (str[0].length() == 0 || str[str.length - 1].length() == 0) {
length--;
}
System.out.println(length);
for (String s1 : str)
if (s1.length() != 0)
System.out.println(s1);
} else {
System.out.println(0);
}
}
}
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.