[백준 10809번] 백준 10809번 :알파벳 찾기(JAVA/자바)

[백준 10809번] 백준 10809번 :알파벳 찾기(JAVA/자바) 

정리용

1. Scanner 이용

package p1;

import java.util.Scanner;

public class Num10809 {
	/*
	 * 백준 번호 : 10809
	 * Scanner 클래스 이용
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] s = new int[26]; //알파벳 전체 수만큼 배열 설정
		for (int i = 0 ; i<s.length;i++) { //s 값 -1 로 초기화
			s[i]=-1;
		}
		String word = sc.nextLine(); //단어 입력받기
		for (int i = 0; i < word.length();i++) {
			char ch = word.charAt(i); //word 의 각 문자 charAt()로 받기
			
			if (s[ch - 'a'] == -1) {
				s[ch - 'a'] = i; //배열s 값 바꾸기
			}
		}
		for (int i = 0;i<s.length;i++) {
			System.out.print(s[i]+" "); //출력
		}
		/* 출력 결과
		 * baekjoon
         * 1 0 -1 -1 2 -1 -1 -1 -1 4 3 -1 -1 7 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
		 */

	}

}