Skip to content

Commit 0e1ab89

Browse files
authored
Add files via upload
1 parent 414b7dc commit 0e1ab89

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

week01/basic/CountCharacter.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package week01.basic;
2+
3+
import java.util.Scanner;
4+
5+
public class CountCharacter {
6+
7+
public static void main(String[] args) {
8+
System.out.println("<문자 등장 횟수 세기>");
9+
// 9단계: 문자 등장 횟수 세기 (대소문자 무시)
10+
// 문제: 사용자로부터 문자열과 한 글자를 입력받아,
11+
// 문자열에서 해당 글자가 몇 번 등장하는지 출력하는 프로그램을 작성하세요.
12+
// (예: 문자열 "Hello World", 문자 'l' -> 3)
13+
// 힌트: String length(), charAt(), toLowerCase(), trim(), for문
14+
// trim() : 불필요한 공백 제거
15+
// toLowerCase() / toUpperCase() : 대소문자 변환
16+
// charAt(index) : index 번지에 있는 문자를 반환 char
17+
int cnt = 0;
18+
Scanner scanner = new Scanner(System.in);
19+
System.out.println("문자열을 입력해주세요.> ");
20+
String str = scanner.nextLine();
21+
System.out.println("한 글자를 입력해주세요.> ");
22+
String ch = scanner.nextLine();
23+
String newStr = str.trim().toLowerCase();
24+
25+
for(int i = 0; i < newStr.length(); i++) {
26+
if(newStr.charAt(i) == ch.trim().toLowerCase().charAt(0)) {
27+
cnt++;
28+
}
29+
}
30+
System.out.println(ch+"는 총 "+cnt+"번 등장합니다.");
31+
scanner.close();
32+
}
33+
34+
}

0 commit comments

Comments
 (0)