문제
해결 방안 고민
•
조건의 알파벳이 영어 문장을 구성하는 알파벳과 일치하면 카운팅 해야 하는 로직 구성이 필요하다.
해결 방법
•
반복문 활용하여 조건 로직 구현한다.
•
비교 기준이 되는 첫 글자 관련 변수를 선언한다.
◦
char spell = arr[0].charAt(0);
•
split() 함수를 활용하여 “ “ 기준으로 문자열을 구분하여 배열을 생성한다.
•
해당 배열의 요소들과 spell 을 비교하여 카운팅해준다.
코드
package algo250325;
import java.io.*;
// 백준 - 도비의 영어 공부 (브론즈 2)
public class Baek2386 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String str = br.readLine();
str = str.toLowerCase();
if (str.equals("#")) {
break;
}
String[] arr = str.split(" ");
char spell = arr[0].charAt(0); // 비교 기준이 되는 첫 글자 관련 변수
int count = 0;
int length = arr.length;
for (int i = 1; i < length; i++) {
for (int j = 0; j < arr[i].length(); j++) {
if (arr[i].charAt(j) == spell) {
count++;
}
}
}
System.out.println(spell + " " + count);
}
}
}
Java
복사