36번***

2023. 9. 5. 10:52javaFestival

import java.util.Scanner;

public class jar36 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		System.out.println("==== 알파벳 빈도수 구하기 ====");
		System.out.print("입력 >> ");
		String str = sc.nextLine();// 띄어쓰기 위해선

		// 담겨있는 글자를 하나씩 쪼개기
		String[] strs = str.split("");

		// 알파벳 개수
		int[] alp = new int[26];

		// 입력한 글자의 갯수만큼 순회하는 for문
		for (int i = 0; i < strs.length; i++) {

			for (char j = 'a'; j <= 'z'; j++) { // strs[i]는 string이고 j는 아스키코드라서 strs[i]==j는 에러가 난다. , 97~122
				if (strs[i].charAt(0) == j) { // String.valueof는 string으로 바뀌어서 나온다. , charAt는 int를 char로 바꾼다., charAt(0)은 첫글자
					alp[j - 'a']++; // j에서 a를 빼주는 이유는 j는 97부터 시작이고 알파벳은 26 즉 0부터 25까지라서 에러가 뜨기 때문이다.
				} else if (strs[i].charAt(0) == j - 32) {
					alp[j - 'a']++;
				}
			}
		}
		char alp2 = 'a';
		for(int a : alp) {
			System.out.println(alp2+" "+a);
			alp2++;
		}
	}
}

'javaFestival' 카테고리의 다른 글

37번  (0) 2023.09.03
35번  (0) 2023.09.03
34번<>  (0) 2023.09.03
33번  (0) 2023.09.03
32번<>  (0) 2023.09.03