HackerRank/JAVA

java String Tokens

예쁜꽃이피었으면 2024. 1. 17. 14:41

https://www.hackerrank.com/challenges/java-string-tokens/problem?isFullScreen=true

 

Java String Tokens | HackerRank

Find the number of alphabetic tokens in a string.

www.hackerrank.com

 

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

 

// Write your code here.  아래 부분만 작성했다가 tc보고 

trim()추가하고 

9번 tc가 input값이 아예없는 거 확인하고 s.length가 0일 때 추가함..

 


https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-

 

String (Java Platform SE 8 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com

 

 

 

 

 

반응형

'HackerRank > JAVA' 카테고리의 다른 글

[작성중]Java Regex 2 - Duplicate Words  (0) 2024.01.19
[못품]Java Regex  (0) 2024.01.17
Java Anagrams  (0) 2024.01.17
Java String Reverse  (0) 2024.01.17
Java Substring Comparisons  (0) 2024.01.17