내가 생각했을때 문제에서 원하는부분
The test file starts with an integer S(1 ≤ S ≤ 100), the number of sentences.
Then follow S lines, each containing a sentence - words of length 1 to 20 separated by spaces.
Every sentence will contain at least one word and be comprised only of characters [a-z][A-Z] and spaces.
No sentence will be longer than 1000 characters.
For each sentence, output the number of consonants and vowels on a line, separated by space.
내가 이 문제를 보고 생각해본 부분
BufferedReader br: 입력을 읽기 위한 객체를 생성한다.
StringBuilder sb: 결과를 저장하기 위한 객체로 생성한다.
문장 개수 입력: 첫 번째 줄에서 문장의 개수 S를 읽어와 정수로 변환해준다.
문자 처리 루프: for문을 사용하여 각 문장을 처리해준다.
각 문장을 sentence 변수에 저장하고, 모음과 자음의 개수를 세기 위해 vowelsCount와 consonantsCount 변수를 0으로 초기화해준다.
문자 루프: sentence의 각 문자에 대해 반복해준다.
Character.isLetter(ch): 문자가 알파벳인지 확인한다.
모음인지 확인하기 위해 "AEIOUaeiou" 문자열에서 문자의 인덱스를 찾는다.
인덱스가 -1이 아닐 경우 모음으로 판단하고 vowelsCount를 증가시켜준다.
그렇지 않으면 자음으로 간주하고 consonantsCount를 증가시켜준다.
결과 저장: 각 문장에 대한 자음과 모음의 개수를 StringBuilder 객체에 추가한다.
각 결과는 공백으로 구분되며, 각 문장의 끝에는 줄바꿈 문자 \n이 추가된다.
결과 출력: StringBuilder에 저장된 내용을 한 번에 출력해준다.
코드로 구현
package baekjoon.baekjoon_25;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 백준 11319번 문제
public class Main895 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
// 첫 번째 줄에서 문장의 개수 S를 읽어옵니다.
int S = Integer.parseInt(br.readLine());
// 각 문장을 처리합니다.
for(int i = 0; i < S; i++) {
String sentence = br.readLine();
int vowelsCount = 0;
int consonantsCount = 0;
// 각 문자에 대해 반복하면서 모음과 자음을 셉니다.
for(char ch : sentence.toCharArray()) {
if(Character.isLetter(ch)) {
// 모음인지 확인
if("AEIOUaeiou".indexOf(ch) != -1) {
vowelsCount++;
} else {
consonantsCount++;
}
}
}
// 결과를 StringBuilder에 추가합니다.
sb.append(consonantsCount).append(" ").append(vowelsCount).append("\n");
}
// 결과를 한 번에 출력합니다.
System.out.print(sb.toString());
br.close();
}
}
코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.