알고리즘 연습
[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);
}
}
}