| 개념 | 사용법 | 예시 | 적용 문제 |
|---|---|---|---|
| 알파벳 → 인덱스 | c - 'a' (소문자)c - 'A' (대문자) | 'c' - 'a' → 2'C' - 'A' → 2 | 10808, 1157 |
| 인덱스 → 알파벳 | (char)(i + 'a')(char)(i + 'A') | (char)(2 + 'A') → 'C' | 1157 |
| 대소문자 변환 | toUpperCase()toLowerCase() | "hello".toUpperCase() → "HELLO" | 1157 |
| 문자열 치환 | replace(old, new) | "dz=ak".replace("dz=", "!") → "!ak" | 2941 |
| 배열 카운팅 | arr[index]++ | alphabet[c - 'a']++ | 10808, 1157 |
10808 코드
package A0study;
import java.io.*;
public class p10808_알파벳개수 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] alphabet = new int[26];
char[] c = br.readLine().toCharArray();
for(int i=0; i<c.length; i++) {
alphabet[c[i] - 'a']++;
}
for(int i=0; i<26; i++) {
System.out.print(alphabet[i] + " ");
}
}
}
1157 코드
package A0study;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p1157_단어공부 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] c = br.readLine().toUpperCase().toCharArray();
int[] alphabet = new int[26];
// 1. 각 알파벳 개수 세기
for (char value : c) {
alphabet[value - 'A']++;
}
// 2. 최댓값 찾기
int max = 0;
for(int i=0; i<26; i++) {
if(alphabet[i] > max) {
max = alphabet[i];
}
}
// 3. 최댓값인 알파벳 찾기 & 중복 체크
char result = '?';
int cnt = 0;
for(int i=0; i<26; i++) {
if(alphabet[i] == max) {
cnt++;
result = (char) (i + 'A');
}
}
// 4단계: 최댓값이 2개 이상이면 ?
if(cnt > 1) {
System.out.println('?');
} else {
System.out.println(result);
}
}
}
2941 코드
package A0study;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p2941_크로아티아알파벳 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] croatia = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
for(String str : croatia) {
input = input.replace(str, "!");
}
System.out.println(input.length());
}
}