코딩테스트/백준

[백준 - 2386] 도비의 영어 공부 .java

머밍 2024. 9. 8. 13:14

https://www.acmicpc.net/problem/2386

 

 

 

내 풀이

1. 몇 줄이 입력되는지 알려주지 않고 입력의 마지막 문자만을 알려준다 -> while문으로 입력받은 문자가 #이면 반복문 종료

 

2. 하나의 소문자와 영어 문장이 공백으로 구분

-> chatAt(0)으로 소문자를, str.substring(2)로 2번째 인덱스(1번째는 공백)부터 저장

-> 대소문자 구분없어야 하기 때문에 입력받은 문장을 소문자로 바꾼다

 

3. 문장을 한글자씩 탐색하여 찾는 소문자와 일치하는지 확인 -> 일치하면 정답 증가

 

 

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str;
        while(true){
            str = sc.nextLine();
            if(str.equals("#")){
                break;
            }
            int answer =0;
            char find = str.charAt(0);//첫 번째 문자를 찾음
            //첫 번째 문자와 공백을 제거, 문자열을 소문자로 변환
            str = str.substring(2).toLowerCase();

            for(char ch : str.toCharArray()){
                if(ch==find){
                    answer++;
                }
            }
            System.out.println(find+ " " +answer);
        }
    }
}

 

 

🧚‍♀️컴공은 마법사다 🧙 도비는 자유다