Java String Tokens

Sort by

recency

|

1772 Discussions

|

  • + 0 comments

    Hello !!

    I'm realy close to solve that problem but test no 9, he didn't give up. i fail that test, the single one. could you help me with an hint? My solution is this:

            Scanner scan = new Scanner(System.in);
        String s = scan.nextLine().trim();
        String[] tokens = s.split("[ !,?._'@]+");
        System.out.println(tokens.length);
        for (int i = 0; i < tokens.length; ++i) {
            System.out.println(tokens[i]);
        }
        scan.close();
    
  • + 0 comments

    well i tried my best unable to pass few test cases. Anyone could help me out??

      public static void myStringTokenizer(String a){
        
            
            
            if(1<=a.length() && a.length() <=(4*100000)){
              //regex
                String regex = "[!,?._@]+";
                String str = a.replaceAll(regex, "");
                str = str.replaceAll("['    ]+", " ");
                String[] s = str.split(" ");
                // System.out.println(Arrays.toString(s));
                System.out.println(s.length);
                for(String temp: s){
                    System.out.println(temp);
                }  
            }
            
        }
    
  • + 0 comments

    I just "improved" (it doesnt use magic number, and I check if its empty the string one time, but I use more memory) another solution I found on this discussion section. the og solution: https://programmingoneonone.com/hackerrank-java-string-tokens-problem-solution.html

    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            final String splitRegEx = "[^a-zA-Z]";
            List<String> listStrSplited = new LinkedList<>();
            
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            scan.close();
            
            String [] draftlistStrSplited = s.split(splitRegEx);
            
            for(String splittedStr : draftlistStrSplited){
                if(!splittedStr.isEmpty()){
                    listStrSplited.add(splittedStr);
                }
            }
            System.out.println(listStrSplited.size());
            for(String splittedStr : listStrSplited){
                System.out.println(splittedStr);
            }
        }
    }
    
  • + 0 comments

    This is a great practice problem for learning how to use regular expressions and string manipulation in Java (or Python). Cricketbuzz login

  • + 0 comments

    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();
        // Write your code here.
        if(!s.isEmpty()) {
        scan.close();
        String[] split = s.split("[ !,?._'@]+");
        System.out.println(split.length);
        for(int i = 0; i < split.length;i++) {
            System.out.println(split[i]);
        } 
    
    } else {
        System.out.println("0");
    }
    

    } }