ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [11319번] Count Me In - Java
    알고리즘 연습 2022. 1. 6. 10:52

     

    1. 배운점

    - trim method는 String의 앞 뒤 whitespace를 제거할 수 있지만, 단어 사이 공백은 따로 처리해줘야 한다.

    - replaceAll method를 활용하면 whitespace를 간단하게 제거할 수 있다.

     

    2. 개선할 점

     

    3. 궁금한 점

     

    4. 풀이

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main {
    	
    	public static void main(String[] args) throws NumberFormatException, IOException {
    		
    		// import java.io.BufferedReader
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		
    		// Read sentences using array
    		int cases = Integer.parseInt(br.readLine());
    		
    		String [] arr = new String[cases];
    		
    		for(int i = 0; i < cases; i ++) {
    			// Remove white space
    			arr[i] = br.readLine().trim().toUpperCase().replaceAll(" ", "");
    		}
    		
    		// Count consonants and vowels
    		for(int i = 0; i < cases; i ++) {
    			int conso = 0;
    			int vowel = 0;
    			
    			for(int j = 0; j < arr[i].length(); j++) {
    				char word = arr[i].charAt(j);
    				if(word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U') {
    					vowel++;
    				}
    			}
    			conso = arr[i].length() - vowel;
    			
    			System.out.println(conso + " " + vowel);
    		}
    	}
    	
    }

     

    '알고리즘 연습' 카테고리의 다른 글

    [1181] 단어 정렬 - Java  (0) 2022.02.08
    [2935] 소음 - Java  (0) 2022.02.07
    [1436번] 영화감독 슘 - Java  (0) 2021.11.23
    [7586번] 덩치 - Java  (0) 2021.11.22
    [2750번] 수 정렬하기 -Java  (0) 2021.11.19
Designed by Tistory.