본문 바로가기
study/Java

[Java] 18. TEST1 풀이 (소문자로 이루어진 문자열의 알파벳의 사용 횟수를 출력하기)

by 금이패런츠 2022. 3. 17.
728x90
반응형
package chap6;
/*
  		String str = 
  "it is possible to draw a parallel between their experience and ours";
  소문자로 이루어진 문자열의 알파벳의 사용 횟수를 출력하기
  [결과]
  a:5개
  b:2개
  c:1개
  d:2개
  ...

 */
public class Test0310_1 {
	public static void main(String[] args) {
    
		String str = "it is possible to draw a parallel between their experience and ours";
		int[] arr= new int[26]; 
		for(int i=0;i<str.length();i++) {
			char ch = str.charAt(i);
			if(ch >= 'a' && ch <= 'z') { 
			   arr[ch-'a']++;
			}
		}
		for(int i=0;i<arr.length;i++) {
			System.out.println((char)(i+'a') + ":" + arr[i] + "개");
		}
	}
}
728x90
반응형